Programs based on multithreading-I



1) Implement a program to define a class hareData having a member count and two synchronize member functions getData() that initialize the value of count and putData() displays value of count. Define two threads and call synchronized methods getData() and putData() respectively.
Code:
             class shareData
             {          
                          int count,d;
                          synchronized void getdata(int c)
                          {
                                       try
                                       {
                                                    Thread.sleep(2000);
                                       }
                                       catch(Exception e)
                                       {          
                                                    System.out.println("I/O error");
                                       }
                                       d=c;
                          }
                          synchronized void putdata()
                          {
                                       try
                                       {
                                                    Thread.sleep(500);
                                       }
                                       catch(Exception e)
                                       {
                                                    System.out.println("I/O error");
                                       }
                                       System.out.println("Value of count="+d);
                          }
             }
             class Thread1 implements Runnable
             {
                          int count;
                          shareData x;
                          Thread1(shareData y,int c)
                          {
                                       x=y;
                                       count=c;
                                       Thread t3=new Thread(this);
                                       t3.start();
                          }
                          public void run()
                          {
                                       x.getdata(count);
                          }
             }
             class Thread2 implements Runnable
             {
                          Thread t4;
                          shareData x;
                          Thread2(shareData y)
                          {
                                       x=y;
                                       t4=new Thread(this);
                                       t4.start();
                          }
                          public void run()
                          {
                                       x.putdata();
                          }
             }
             class Sharedatamain
             {
                          public static void main(String ar[])
                          {          
                                       shareData s=new shareData();
                                       Thread1 s1=new Thread1(s,10);
                                       Thread2 s2=new Thread2(s);
                          }
             }
Output:
             C:\Program Files\Java\jdk1.6.0_07\bin>javac Studentmain.java
             C:\Program Files\Java\jdk1.6.0_07\bin>java Studentmain
             Value of count=10

2) Program based on creating threads by extending thread class.


Code:
            
class ExtendedThread extends Thread
             {
                          private String str;
                          public ExtendedThread(String str)
                          {
                                       this.str=str;
                                       System.out.println("\nchild thread"+this.str);
                          }
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println("Message from child thread="+i);
}
System.out.println("\n Bye Bye child thread");
}
}
class ExtendThreadMain
{
public static void main(String args[])
{
ExtendedThread Et=new ExtendedThread("child");
Et.start();
for(int j=0;j<=5;j++)
{
System.out.println("Message from main thread="+j);
}
System.out.println("\n Bye Bye main thread");
}
}

Output:
child threadchild
Message from main thread=0
Message from child thread=0
Message from main thread=1
Message from child thread=1
Message from main thread=2
Message from child thread=2
Message from main thread=3
Message from child thread=3
Message from main thread=4
Message from child thread=4
Message from main thread=5
Message from child thread=5
 Bye Bye child thread
 Bye Bye main thread



3) Program based on creating threads by implementing Runnable Interface.

Code:
            
class RunnableThread implements Runnable
             {
                          private String str;
                          public RunnableThread(String str)
                          {
                                       this.str=str;
                          }
                          public void run()
                          {
for(int i=0;i<=5;i++)
{
System.out.println("Message from child thread="+i);
}
System.out.println("\n End of child thread");
}
}
class RunnableThreadDemo
{
public static void main(String args[])
{
RunnableThread Rt=new RunnableThread("child thread");
Thread T=new Thread(Rt);
T.start();
for(int j=0;j<=5;j++)
{
System.out.println("Message from main thread="+j);
}
System.out.println("\n End of main thread");
}
}

Output:
Message from main thread=0
Message from child thread=0
Message from main thread=1
Message from child thread=1
Message from main thread=2
Message from child thread=2
Message from main thread=3
Message from child thread=3
Message from main thread=4
Message from child thread=4
Message from main thread=5
Message from child thread=5
 End of main thread
 End of child thread


4) Program based on creating threads and use of getName() and setName() methods of thread class.



Code:
            
class A extends Thread
             {
                          public void run()
                          {
                                       for(int i=0;i<=5;i++)
                                       {
                                                    if(i==3)
                                                                 {
                                                                              yield();
}
System.out.println("\n"+this.getName()+":"+i);
}
System.out.println("\n"+this.getName()+"is Exit");
}
}
class B extends Thread
{
public void run()
{
for(int j=0;j<=5;j++)
{
System.out.println("\n"+this.getName()+":"+j);
}
System.out.println("\n"+this.getName()+"is Exit");
}
}
class C extends Thread
{
public void run()
{
for(int k=0;k<=5;k++)
{
System.out.println("\n"+this.getName()+":"+k);
try
{
sleep(500);
}
catch(InterruptedException e)
{
System.out.println("\n"+this.getName()+"Interrupted");
}
}
System.out.println("\n"+this.getName()+"is Exit");
}
}
class ThreadMain extends Thread
{
public static void main(String args[])
{
A Athread=new A();
B Bthread=new B();
C Cthread=new C();
Athread.setName("First Thread");
Bthread.setName("Second Thread");
Cthread.setName("Third Thread");
Athread.start();
Bthread.start();
Cthread.start();
System.out.println("\n main thread started");
for(int l=0;l<=5;l++)
{
System.out.println("main thread Message="+l);
try
{
sleep(500);
}
catch(InterruptedException e)
{
System.out.println("main Thread Interrupted");
}
}
System.out.println("\n Bye Bye main thread");
}
}

Output:
First Thread:0
First Thread:1
Second Thread:0
main thread started
Second Thread:1
First Thread:2
Third Thread:0
First Thread:3
Second Thread:2
main thread Message=0
Second Thread:3
First Thread:4
Second Thread:4
Second Thread:5
Second Thread is Exit
First Thread:5
First Thread is Exit
Third Thread:1
main thread Message=1
main thread Message=2
Third Thread:2
main thread Message=3
Third Thread:3
main thread Message=4
Third Thread:4
main thread Message=5
Third Thread:5
Third Threadis Exit
Bye Bye main thread



5) Program based on creating threads and use of join() and setPriority() methods with implementing Runnable interface.

Code:
            
class clicker implements Runnable
             {
                          int click=0;
                          Thread t;
                          private boolean running =true;
                          public clicker(int p)
                          {
                                       t=new Thread(this);
                                       t.setPriority(p);
                          }
public void run()
{
while(running)
{
click++;
}
}
public void stop()
{
running=false;
}
public void start()
{
t.start();
}
}
class HiLowPriority
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi=new clicker(Thread.NORM_PRIORITY+2);
clicker lo=new clicker(Thread.NORM_PRIORITY-2);
lo.start();
hi.start();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted.");
}
lo.stop();
hi.stop();
try
{
hi.t.join();
lo.t.join();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught.");
}
System.out.println("Low Priority thread;"+lo.click);
System.out.println("High Priority thread;"+hi.click);
}
}

Output:
Low Priority thread;412112288
High Priority thread;417602591


6) Program based on creating threads and use of join() and setPriority() methods with extending Thread class.

Code:

class Athread extends Thread
             {
                          public void run()
                          {
                                       for(int i=0;i<=5;i++)
                                       {
                                                    System.out.println("\n"+this.getName()+"="+i);
                                       }
System.out.println("\n"+this.getName()+" Exited");
}
}
class Bthread extends Thread
{
public void run()
{
for(int j=0;j<=5;j++)
{
System.out.println("\n"+this.getName()+":"+j);
}
System.out.println("\n"+this.getName()+" Exited");
}
}
class Cthread extends Thread
{
public void run()
{
for(int k=0;k<=5;k++)
{
System.out.println("\n"+this.getName()+":"+k);
}
System.out.println("\n"+this.getName()+" Exited");
}
}
class PriorityThreadDemo
{
public static void main(String args[])
{
Athread A=new Athread();
Bthread B=new Bthread();
Cthread C=new Cthread();
A.setName("One");
A.start();
System.out.println("\nThread One started");
B.setName("Two");
B.start();
System.out.println("\nThread Two started");
C.setName("Three");
C.start();
System.out.println("\nThread Three started");
A.setPriority(Thread.MAX_PRIORITY);
B.setPriority(Thread.MIN_PRIORITY);
C.setPriority(Thread.NORM_PRIORITY);
for(int l=0;l<=5;l++)
{
System.out.println("main thread Message="+l);
try
{
A.join();
B.join();
C.join();
}
catch(InterruptedException e)
{System.out.println("main Thread Interrupted");}
}
System.out.println("\n Bye Bye main thread");
}
}

Output:
Thread One started
One=0
One=1
One=2
Thread Two started
One=3
One=4
One=5
Two:0
Thread Three started
Two:1
Three:0
One Exited
Three:1
Three:2
Three:3
Three:4
Three:5
Three Exited
Two:2
main thread Message=0
Two:3
Two:4
Two:5
Two Exited
main thread Message=1
main thread Message=2
main thread Message=3
main thread Message=4
main thread Message=5
Bye Bye main thread


7) Program to create Threads and using constructors of  Thread class.

Code:

class NameThread extends Thread
             {
                          private String name;
                          public NameThread(String str)
                          {
                                       super(str);
                                       name=str;
                                       System.out.println("\nchild thread"+name);
start();
}
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println("Message from child thread="+i);
}
System.out.println("\n Bye Bye child thread");
}
}
class NameThreadMain
{
public static void main(String args[])
{
NameThread NT1=new NameThread("First Thread");
NameThread NT2=new NameThread("Second Thread");
NameThread NT3=new NameThread("Third Thread");
System.out.println("\n main thread started");
for(int j=0;j<=5;j++)
{
System.out.println("Message from main thread="+j);
}
System.out.println("\n Bye Bye main thread");
}
}

Output:
child threadFirst Thread
child threadSecond Thread
Message from child thread=0
Message from child thread=1
hild threadThird Thread
Message from child thread=2
Message from child thread=0
Message from child thread=1
Message from child thread=3
Message from child thread=2
main thread started
Message from child thread=3
Message from child thread=0
Message from child thread=4
Message from child thread=1
Message from child thread=4
Message from main thread=0
Message from child thread=5
Message from child thread=2
Message from child thread=5
Bye Bye child thread
Message from child thread=3
Message from child thread=4
Message from child thread=5
Bye Bye child thread
Bye Bye child thread
Message from main thread=1
Message from main thread=2
Message from main thread=3
Message from main thread=4
Message from main thread=5
Bye Bye main thread


8) Program demonstrating isAlive() and join() methods.

Code:
 class A1 extends Thread
             {
                          public void run()
                          {
                                       for(int i=0;i<=5;i++)
                                       {
                                                    if(i==3)
                                                                 {yield();}
                                                    System.out.println("\n"+this.getName()+":"+i);
}
System.out.println("\n"+this.getName()+"is Exit");
}
}
class B1 extends Thread
{
public void run()
{
for(int j=0;j<=5;j++)
{
System.out.println("\n"+this.getName()+":"+j);
}
System.out.println("\n"+this.getName()+"is Exit");
}
}
class C1 extends Thread
{
public void run()
{
for(int k=0;k<=5;k++)
{
System.out.println("\n"+this.getName()+":"+k);
try{sleep(500);}
catch(InterruptedException e)
{System.out.println("\n"+this.getName()+"is Interrupted");}
}
System.out.println("\n"+this.getName()+"is Exit");
}
}
class IsAliveJoinDemo
{
public static void main(String args[])
{
A1 Athread=new A1();
B1 Bthread=new B1();
C1 Cthread=new C1();
Athread.setName("Thread no 1");
Bthread.setName("Thread no 2");
Cthread.setName("Thread no 3");
System.out.println("\n Thread no 1 started");
Athread.start();
System.out.println("\n Thread no 2 started");
Bthread.start();
System.out.println("\n Thread no 3 started");
Cthread.start();
System.out.println("\n Thread no 1="+Athread.isAlive());
System.out.println("\n Thread no 2="+Bthread.isAlive());
System.out.println("\n Thread no 3="+Cthread.isAlive());
System.out.println("\n main thread");
for(int l=0;l<=5;l++)
{
System.out.println("main thread Message="+l);
try
{
Athread.join();
Bthread.join();
Cthread.join();
}
catch(InterruptedException e)
{System.out.println("main Thread Interrupted");}
}
System.out.println("\nmain thread Exited");
System.out.println("\n Thread no 1="+Athread.isAlive());
System.out.println("\n Thread no 2="+Bthread.isAlive());
System.out.println("\n Thread no 2="+Cthread.isAlive());
}
}

Output:
Thread no 1 started
Thread no 2 started
Thread no 1:0
Thread no 3 started
Thread no 1:1
Thread no 2:0
Thread no 1:2
Thread no 2:1
Thread no 2:2
Thread no 2:3
Thread no 3:0
Thread no 1=true
Thread no 1:3
Thread no 1:4
Thread no 1:5
Thread no 1is Exit
Thread no 2:4
Thread no 2:5
Thread no 2is Exit
Thread no 2=true
Thread no 3=true
main thread
main thread Message=0
Thread no 3:1
Thread no 3:2
Thread no 3:3
Thread no 3:4
Thread no 3:5
Thread no 3is Exit
main thread Message=1
main thread Message=2
main thread Message=3
main thread Message=4
main thread Message=5
main thread Exited
Thread no 1=false
Thread no 2=false
Thread no 2=false



9) Program based on creating threads and use of suspend() , sleep() and resume() methods.
 Code:
class childThread implements Runnable
             {          
                          String name;
                          boolean flag=false;
                          Thread t;
                          childThread(String fname)
                          {
                                       name=fname;
                                       t=new Thread(this,name);
System.out.println("\n New Thread="+t);
t.start();
}
public void run()
{
try
{
for(int i=0;i<=5;i++)
{
System.out.println("Name="+i);
Thread.sleep(500);
synchronized(this)
{
while(flag)
{wait();}
}
}
}
catch(InterruptedException e)
{
System.out.println("\n"+name+"is Exiting");
}
}
void ThreadSuspend()
{
flag=true;
}
synchronized void ThreadResume()
{
flag=false;
notify();
}
}
class SuspendResume
{
public static void main(String args[])
{
childThread TC1=new childThread("One");
childThread TC2=new childThread("Two");
try
{
Thread.sleep(500);
System.out.println("\nOne Suspended");
TC1.ThreadSuspend();
System.out.println("\nOne Resume");
TC1.ThreadResume();
System.out.println("\nTwo Suspended");
TC2.ThreadSuspend();
Thread.sleep(500);
System.out.println("\nTwo Resume");
TC2.ThreadResume();
}
catch(InterruptedException e)
{
System.out.println("main Thread Interrupted");
}
try
{
System.out.println("\nWaiting for thread to finish");
TC1.t.join();
TC2.t.join();
}
catch(InterruptedException e)
{
System.out.println("main Thread Interrupted");
}
System.out.println("\n Bye Bye main thread");
}
}

Output:
New Thread=Thread[One,5,main]
New Thread=Thread[Two,5,main]
Name=0
Name=0
Name=1
One Suspended
One Resume
Two Suspended
Name=1
Two Resume
Name=2
Name=2
Waiting for thread to finish
Name=3
Name=3
Name=4
Name=4
Name=5
Name=5
Bye Bye main thread


10) Program for thread creating and creating synchronize methods.


 Code:
class ThreadSynchronized1
             {          
                          private int ctr=0;
                          synchronized public void ThreadSynchronizedincrement()
                          {
                                       ctr++;
                                       try
                                       {
                                                    Thread.sleep(500);
                                       }
catch(InterruptedException e)
{
System.out.println("Thread child Interrupted");
}
System.out.println("\n Counter="+ctr);
}
}
class ThreadSynchronizedCounter1 extends Thread
{
ThreadSynchronized1 tsc;
ThreadSynchronizedCounter1(ThreadSynchronized1 ts1)
{
tsc=ts1;
}
public void run()
{
tsc.ThreadSynchronizedincrement();
}
}
class ThreadSynchronizedCountermain1
{
public static void main(String args[])
{
ThreadSynchronized1 TSC=new ThreadSynchronized1();
ThreadSynchronizedCounter1 TSctr1=new ThreadSynchronizedCounter1(TSC);
ThreadSynchronizedCounter1 TSctr2=new ThreadSynchronizedCounter1(TSC);
System.out.println("\n Thread synchronised counter no1 started");
TSctr1.start();
System.out.println("\n Thread synchronised counter no2 started");
TSctr2.start();
try
{
TSctr1.join();
TSctr2.join();
}
catch(InterruptedException e)
{
System.out.println("main Thread Interrupted");
}
System.out.println("\n Bye Bye main thread");
}
}

Output:
Thread synchronised counter no1 started

Thread synchronised counter no2 started

Counter=1

Counter=2

Bye Bye main thread






No comments:

Post a Comment