Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

qwer

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);
   }   
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.