How to run a thread after another?

后端 未结 3 499
一整个雨季
一整个雨季 2021-01-28 00:22

Create three threads and the main thread. Execute each thread as simultaneous tasks. Display information when exiting each thread.

3条回答
  •  遇见更好的自我
    2021-01-28 01:12

    public class sync extends Thread {

    public void run() {
    
        synchronized (this) {
            for (int i = 5; i > 0; i--) {
                System.out.print("Thread Name :" + Thread.currentThread().getName() + i+"\n");
            }
        }
    }
    

    }

    class demo {

    public static void main(String args[]) {
        sync obj1 =new sync();
        sync obj2 =new sync();
        sync obj3 =new sync();
        obj1.setName("First");
        obj2.setName("Second");
        obj3.setName("Third");
        obj1.start();
        obj2.start();
        obj3.start();
    }
    

    }

    O/p:
    

    Thread Name :First5 Thread Name :First4 Thread Name :First3 Thread Name :First2 Thread Name :First1 Thread Name :Second5 Thread Name :Second4 Thread Name :Second3 Thread Name :Second2 Thread Name :Second1 Thread Name :Third5 Thread Name :Third4 Thread Name :Third3 Thread Name :Third2 Thread Name :Third1

    HOPE THIS HELPS :)

提交回复
热议问题