0% found this document useful (0 votes)
60 views

Practical: 10 Write A Program For Thread Synchronization. Algorithm

The document provides a 13 step algorithm to create a program for thread synchronization in Java. It involves creating a MyThread class that extends Thread and overrides the run method, a SynchronizedOutput class with a static synchronized displayList method to print thread messages, and a ThreadSynchronization class with a main method that creates MyThread objects and checks their status.

Uploaded by

himanshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Practical: 10 Write A Program For Thread Synchronization. Algorithm

The document provides a 13 step algorithm to create a program for thread synchronization in Java. It involves creating a MyThread class that extends Thread and overrides the run method, a SynchronizedOutput class with a static synchronized displayList method to print thread messages, and a ThreadSynchronization class with a main method that creates MyThread objects and checks their status.

Uploaded by

himanshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL: 10

Write a program for thread synchronization.

Algorithm:
Step 1: Start.

Step 2: Create a class name MyThread which inherits the class Thread.

Step 3: Declare and define an array message of type string and declare it static.

Step 4: Create a constructor of the class MyThread to set the value of the id.

Step 5: Create the void run() method to display the name of the thread and the message.

Step 6: Create randomWait() method and call sleep method in that method to make thread
wait for a particular interval of time.

Step 7: Create another class named SynchronizedOutput and in this class create a static
synchronized displayList() method which takes two string arguments name and list.

Step 8: In the displayList() method call the currentThread() and the randomWait() method and
print the values of name and the list.

Step 9: Create another class named ThreadSynchronization in which main method is created.

Step 10: In main method create the objects of the MyThread class and call the start() methid by
the objects of the class MyThread.

Step 11: Declare two variables thread1isAlive and thread2isAlive of type Boolean and initialize
them as true.

Step 12: Check if thread1 or thread2 is alive or dead and print the same.

Step 13: Stop.


PROGRAM:
class MyThread extends Thread

static String message[] ={ "Eat", "Well", "Sleep", "Well", "Do", "WELL."};

public MyThread(String id)

super(id);

public void run()

SynchronizedOutput.displayList(getName(),message);

void randomWait()

try

sleep((long)(3000*Math.random()));

catch (InterruptedException x)

System.out.println("Interrupted!");

}
class SynchronizedOutput

public static synchronized void displayList(String name,String list[])

for(int i=0;i<list.length;++i) {

MyThread t = (MyThread)Thread.currentThread();

t.randomWait();

System.out.println(name+list[i]);

class ThreadSynchronization

public static void main(String args[])

MyThread thread1 = new MyThread("thread1: ");

MyThread thread2 = new MyThread("thread2: ");

thread1.start();

thread2.start();

boolean thread1IsAlive = true;

boolean thread2IsAlive = true;

do {

if (thread1IsAlive && !thread1.isAlive()) {

thread1IsAlive = false;
System.out.println("Thread 1 is dead.");

if (thread2IsAlive && !thread2.isAlive()) {

thread2IsAlive = false;

System.out.println("Thread 2 is dead.");

} while(thread1IsAlive || thread2IsAlive);

}
OUTPUT:

You might also like