class ThreadDemo extends Thread { private Thread t; private String threadName; ThreadDemo( String name) { threadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " + threadName ); for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Let the thread sleep for a while. } System.out.println("Thread " + threadName + " exiting."); } public void start (int i) { System.out.println("Starting " + threadName ); if (t == null) { if(i==1){ t = new Thread (this, threadName); t.setPriority(10); t.start (); } else{ t = new Thread (this, threadName); t.setPriority(2); t.start (); } } } } public class TestThread { public static void main(String args[]) { ThreadDemo T1 = new ThreadDemo( "Thread-1"); T1.start(1); ThreadDemo T2 = new ThreadDemo( "Thread-2"); T2.start(2); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more