Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

you were asked to spawn and sychronize 20 processes , each with a single thread

ID: 671807 • Letter: Y

Question

you were asked to spawn and sychronize 20 processes, each with a single thread of control, to produce the desired output. In this problem, you are asked to write a Java program /home/<logname>/homeworks/hw3/FiveAlive.java as one process with 20 threads of control, and spawn and sychronize those threads to print the following to standard error (of course, with different thread ids):

Each line must be printed to standard error by a different thread, and you must not use any Java synchronization mechanisms not presented in class. In other words, do not use any Java constructs not presented in class. Do not use sleep, wait, await, yield, or any other similar method. Also, the threads need not print in the order in which they were created, but they must alternate between printing five and alive.

Explanation / Answer

class MyThread1 implements Runnable
{
Thread t;
MyThread1(String s)
{

t=new Thread(this,s);
t.start();

}
  
public void run()
{
for(int i=0;i<20;i++)
{
System.out.println("Thread Name :"+Thread.currentThread().getName());
try
{
Thread.sleep(1000);
}catch(Exception e){}
}
}
}

public class RunnableThread1
{
public static void main(String args[])
{
System.out.println("Thread Name :"+Thread.currentThread().getName());   
MyThread1 m1=new MyThread1("five");
MyThread1 m2=new MyThread1("alive");

}

}