Pages

Ads 468x60px

Showing posts with label thread. Show all posts
Showing posts with label thread. Show all posts

Monday, 17 December 2012

implementation of 3 threads using run and also runnable



25.WAJP that creates 3 threads by extending Thread class. First thread displays “Good Morning” every 1 sec, the second thread displays “Hello” every 2 seconds and the third displays “Welcome” every 3 seconds. (Repeat the same by implementing Runnable)
Program:
                                     Program for 3 threads

for the above programs use join() if it is necessary, because in the java if parent dies so as the child also if not child behaves by giving different -different values
 two ways i have written the program:
import java.lang.*;
import java.util.*;
import java.awt.*;
class One implements Runnable
{
One()
{
new Thread(this,"one").start();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
public void run()
{
for(;;)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
System.out.println("GOOD MORNING");
}
}
}
class Two implements Runnable
{
Two()
{
new Thread(this,"two").start();
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
}
}
public void run()
{
for(;;)
{
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
}
System.out.println("HELLO");
}
}
}
class Three implements Runnable
{
Three()
{
new Thread(this,"Three").start();
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
}
}
public void run()
{
for(;;)
{
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
}
System.out.println("WELCOME");
}
}
}
class MyThread
{
public static void main(String args[])
{
One obj1=new One();
Two obj2=new Two();
Three obj3=new Three();
}
}


ALTERNATIVE:

class One extends Thread
{
public void run()
{
for(;;)
{
try
{
sleep(1000);
}
catch(InterruptedException e)
{
}
System.out.println("good morning");
}
}
}
class Two extends Thread
{
public void run()
{
for(;;)
{
 try
{
sleep(2000);
}
catch(InterruptedException e)
{
}
System.out.println("hello");
}
}
}
class Three extends Thread
{
public void run()
{
for(;;)
{
 try
{
sleep(3000);
}
catch(InterruptedException e)
{
}
System.out.println("Welcome");
}
}
}
class MyThread
{
public static void main(String args[])
{
Thread t=new Thread();
One obj1=new One();
Two obj2=new Two();
Three obj3=new Three();
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
Thread t3=new Thread(obj3);
t1.start();
try
{
t.sleep(1000);
}
catch(InterruptedException e)
{
}
t2.start();
try
{
t.sleep(2000);
}
catch(InterruptedException e)
{
}
t3.start();
try
{
t.sleep(3000);
}
catch(InterruptedException e)
{
}
}
}

Producer consumer problem using inter thread communication



18. WAJP that correctly implements Producer-Consumer problem using the concept of Inter Thread Communication.
Program:
                                         program for inter thread communication
class Producer extends Thread
{
  static int i=0;
 
  Consumer obj;
 
  Producer(Consumer obj1)
  {
   obj=obj1;
  }
 
  public void run()
  {
   System.out.println("producer thread started");
  
   synchronized(obj)
   {
  
    while(true)
    {
    try
  
     {
      System.out.println("Value produced is:"+(i+1));
     
      obj.j=++i;
     
      Thread.sleep(10);
     
      wait();
     
      System.out.println("lock is released by producer");
    }catch(Exception e)
    {
    }
   }
  }
 }
}

class Consumer extends Thread
{
 int j;

 public void run()
 {
  System.out.println("consumer thread has been started");
 
  synchronized(this)
  {
   while(true)
   {
    try
    {
     System.out.println("value of j before exception"+j);
    
     Thread.sleep(10);
    
     System.out.println("consumed:"+j);
    
     notify();
   }catch(Exception e)
   {
   }
  }
 }
}

}
class CommDemo
{
 public static void main(String[] args)throws Exception
 {
  Consumer con=new Consumer();
 
  Producer pod=new Producer(con);
 
  con.start();
 
  Thread.sleep(5);
 
  pod.start();
 }
}           

Life cycle of thread

 WAJP demonstrating the life cycle of a thread.
Program:
                                   Download link for life cycle of thread
class MyThreadDemo extends Thread
{
 public MyThreadDemo()
     {
    System.out.println(this.getName()+"is newly born");
   }
 public void run()
   {  
  try
   { 
    int i=0;
      while(i<3)
     System.out.println(this.getName()+"Thread is in running state:");
   i++;
   System.out.println("Thread going to sleeping static"); 
  Thraed.sleep(2000);
   }
  System.out.println("Thread completed running state & Thread completed");
 }
 catch(InterruptedException ie)
 ie.print stackTrace();
  {
   }
  }
 }
class StateDemo
 {
 public static void main(String[] args)
  {
 System.out.println("Inside main Thread"); 
   MyThreadDemo obj=new MyThreadDemo();
     obj.setName("T1");
     obj.start(); 
   }
}
 

Sample text

Sample Text

Sample Text