Showing posts with label Threads. Show all posts
Showing posts with label Threads. Show all posts

May 31, 2011

Java threads yield() method

The yield() method is a static method on the Thread class that causes the currently executing thread to go to the runnable state, yielding to a higher priority thread to become the current executing thread. Whether execution of the yield() method will produce the desired result is the prerogative of the Thread Scheduler.


package info.icontraining.threads;

public class MyRunnable implements Runnable {
 
   public static void main(String[] args) {
      Thread t = new Thread(new MyRunnable());
      t.start();
  
      for(int i=0; i<50; i++) {
         System.out.println("Inside main");
      }
   }
 
   public void run() {
      for(int i=0; i<50; i++) {
         System.out.println("Inside run");
         Thread.yield();
      }
   }
}

April 1, 2011

Thread Join - joining at the end of a thread

The join() method on a Thread instance is used to join the current thread at the end of the thread on which the  join is invoked. That is, the current thread will suspend its execution (and will not resume execution) until the thread instance on which the join is invoked completes its execution.

In the example code below, the 2 thread instances t1 and t2 are names run-1 and run-2 respectively and invoked. In the run, the join() method is invoked on the thread instance t2 if the currently executing thread is t1, that is, thread t1 joins on thread t2. Therefore, execution of thread t1 is suspended and does not resume until thread t2 completes execution. After t2 completes its execution, thread t1 can then resume its execution.


package info.icontraining.threads;

public class Test implements Runnable {

   private static Thread t1, t2;
 
   public void run() {
      try {
         if (Thread.currentThread().getName().equals("run-1") {
            t2.join();
         }
   
         for (int i=0;i<50;i++) {
            System.out.println(i + ": In " + 
               Thread.currentThread().getName() + " thread");
         }
   
      } catch (InterruptedException e) {
         e.printStackTrace();
      }    
   }
     
   public static void main(String[] args)
             throws InterruptedException {
  
      t1 = new Thread(new Test());
      t1.setName("run-1");
      t1.start();
  
      t2 = new Thread(new Test());
      t2.setName("run-2");
      t2.start();
   }
}

February 8, 2011

Java Threads Basic Example

Creating a new thread of execution by extending the Thread class

public class MyThread extends Thread {

   public void run() {
      for(int i=0; i<20; i++) {
         System.out.println("In thread: " + Thread.currentThread().getName());
      }
   }
}

public class Driver {

   public static void main(String[] args) {

      MyThread t = new MyThread();
      t.setName("MyThread");
      t.start();

      for(int i=0; i<20; i++) {
         System.out.println("In thread: " + Thread.currentThread().getName());
      }
   }
}

Creating a new thread of execution by implementing the Runnable interface

public class MyRunnable implements Runnable {

   public void run() {
      for(int i=0; i<20; i++) {
         System.out.println("In thread: " + Thread.currentThread().getName());
      }
   }
}

public class Driver {

   public static void main(String[] args) {

      MyRunnable r = new MyRunnable();
      Thread t = new Thread(r);
      t.setName("MyRunnable");
      t.start();

      for(int i=0; i<20; i++) {
         System.out.println("In thread: " + Thread.currentThread().getName());
      }
   }
}

The second method (implementing Runnable) is preferred over the first method (extending Thread)