Question: Homework 4 1 . ( 1 0 pts ) For the following program, explain the interesting elements related to threads. Focus on explaining the output
Homework
pts For the following program, explain the interesting elements related to threads. Focus on
explaining the output of the program.
public class TaskThreadDemo
public static void main String args
String sa aX;
for String s: sa
Runnable ps new PrintChar s;
Thread ts new Thread ps s;
tsstart ;
end for each character
end main
end class TaskThreadDemo
class PrintChar implements Runnable
String ch;
int times;
public PrintChar String c int n
ch c;
times n;
end constructor
public void run
for int i ; i times; i
System.out.print ch;
end for loop
end method run
end class PrintChar
pts What is changed if the method called on line start is replaced with run Explain of
course Focus on explaining the output of the program.
pts What is changed if the method Thread.yield is added between lines and Explain.
Focus on explaining the output of the program.
pts Modify the above program so that the Thread.sleep method is called after each character has
been printed causing it to sleep for milliseconds. Describe how that modification has altered the
output and explain why the change had the effect that you described.
pts Modify the above program so that the Thread.sleep method is called after each thread is
created in the main method causing it to sleep for milliseconds. Describe how that modification has
altered the output and explain why the change had the effect that you described
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
