creating threads through annomyous inner class

后端 未结 3 887
情深已故
情深已故 2021-02-10 09:25

I was developing the code of creating a thread but without extending the thread class or implementing the runnable interface , that is through anonymous inner classes ..

3条回答
  •  后悔当初
    2021-02-10 09:52

    Anonymous inner classes are the classes without a name, means there is no explicit name for it, but JVM name it as Mythread3$1 for referencing its objects. so when you print th.getClass() and th.getClass().getSuperclass() you will get output as MyThread3$1 and Thread.

    you can create Anonymous inner classes by extending Thread class or any of its subclasses (the other implementing Runnable interface or any of its subtypes). In the first piece of code, you extended thread class. That's why you got class name as MyThread3$1( as it is Anonymous inner class) and superclass as Thread class( as you extended it). so you can create as many anonymous inner classes extending a thread class and JVM names them as MyThread3$1, MyThread3$2, MyThread3$3...... But when you call start method each thread will execute their run method only(which you overrode in MyThread3$1, MyThread3$2 by extending thread class).

    package com.tej.threads;
    
    public class MyThread3 {
    public static void main(String... a) {
    
        Thread th = new Thread() {
    
            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
    
                        System.out.println(i + "\t" + ".." + Thread.currentThread().getId());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            }
    
        };
        Thread th1 = new Thread() {
    
            public synchronized void run() {
                for (int i = 50; i < 70; i++) {
                    try {
    
                        System.out.println(i + "\t" + ".."+ Thread.currentThread().getId());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            }
    
        };
        th.start();
        try {
            th.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        th1.start();
        System.out.println(th.getClass()+ " " + th.getClass().getSuperclass());
        for(int i=0;i<20;i++)
        {
            System.out.println("i am main Thread");
        }
    
    
      }
    }
    

    output is

    0   ..8
    1   ..8
    2   ..8
    3   ..8
    4   ..8
    5   ..8
    6   ..8
    7   ..8
    8   ..8
    9   ..8
    10  ..8
    11  ..8
    12  ..8
    13  ..8
    14  ..8
    15  ..8
    16  ..8
    17  ..8
    18  ..8
    19  ..8
    class com.tej.threads.MyThread3$1 class java.lang.Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread  
    i am main Thread
    i am main Thread 
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    i am main Thread
    50  ..9
    51  ..9
    52  ..9
    53  ..9
    54  ..9
    55  ..9
    56  ..9
    57  ..9
    58  ..9
    59  ..9
    60  ..9
    61  ..9
    62  ..9
    63  ..9
    64  ..9
    65  ..9
    66  ..9
    67  ..9
    68  ..9
    69  ..9
    

    you can clearly see thread1 will print 1 to 20 with its id, Thread2 will print 50 to 70 with its id that means each thread is executing its own run method. Note: The main thread will execute program line by line if it encounters a th.start method then main thread will send child thread to execute its run method and main thread goes to next line for execution.

提交回复
热议问题