1. (20 pts) For the following program, explain the interesting elements related to threads. Focus on explaining the output of the program.
1 public class TaskThreadDemo {
2 public static void main (String args []) {
3 String [] sa = {“a”, “X”, “+”, “.”};
4 for (String s: sa) {
5 Runnable ps = new PrintChar (s, 200);
6 Thread ts = new Thread (ps, s);
7 ts.start ();
8 } // end for each character
9 } // end main
10 } // end class TaskThreadDemo
11
12 class PrintChar implements Runnable {
13 String ch;
14 int times;
15
16 public PrintChar (String c, int n) {
17 ch = c;
18 times = n;
19 } // end constructor
20
21 public void run () {
22 for (int i = 0; i < times; i++) {
23 System.out.print (ch);
24 } // end for loop
25 } // end method run
26 } // end class PrintChar
2. (20 pts) What is changed if the method called on line 7, start(), is replaced with run()? Explain (of course). Focus on explaining the output of the program.
3. (20 pts) What is changed if the method Thread.yield() is added between lines 23 and 24? Explain. Focus on explaining the output of the program.
4. (20 pts) Modify the above program so that the Thread.sleep method is called after each character has been printed causing it to sleep for 500 milliseconds. Describe how that modification has altered the output and explain why the change had the effect that you described.
5. (20 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 500 milliseconds. Describe how that modification has altered the output and explain why the change had the effect that you described.
1
Grading Rubric:
Attribute Meets Does not meet
Problem 1 20 points
Explains the interesting elements related to threads. Focuses on explaining the output of the program. 0 points
Does not explain the interesting elements related to threads. Does not focus on explaining the output of the program.
Problem 2 20 points
Explains what is changed if the method called on line 7, start(), is replaced with run().Focuses on explaining the output of the program. 0 points
Does not explain what is changed if the method called on line 7, start(), is replaced with run(). Does not focus on explaining the output of the program.
Problem 3 20 points
Explains what is changed if the method Thread.yield() is added between lines 23 and 24. Focuses on explaining the output of the program. 0 points
Does not explain what is changed if the method Thread.yield() is added between lines 23 and 24. Does not focus on explaining the output of the program.
Problem 4 20 points
Explains how the output is changed if the Thread.sleep method is called after each character has been printed. 0 points
Does not explain how the output is changed if the Thread.sleep method is called after each character has been printed.
Problem 5 20 points
Explains how the output is changed if the Thread.sleep method is called after each thread is created in the main method. 0 points
Does not explain how the output is changed if the Thread.sleep method is called after each thread is created in the main method.
2

![[SOLVED] Cmsc335 – homework 4](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[SOLVED] Cmsc335 – 4. (20 pts) (ex 1.8.2) the dining philosophers problem was invented by e. w. dijkstra, a concurrency pioneer, to clarify the notions of deadlock and starvation freedom. imagine five philosophers who spend their lives just thinking and feasting. they sit around a circular table with five chairs. the table has a big plate of rice. however, there are only five chopsticks (in the original formulation forks) available, as shown in fig. 1.5. each philosopher thinks. when he gets hungry, he sits down and picks up the two chopsticks that are closest to him. if a philosopher can pick up both chopsticks, he can eat for a while. after a philosopher finishes eating, he puts down the chopsticks and again starts to think.](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.