I need answer to question 4 and 5 please and dont just post anything off the int
ID: 3829278 • Letter: I
Question
I need answer to question 4 and 5 please and dont just post anything off the internet .
1. (20 pts) For the following program, explain the interesting elements related to threads.
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
4. (20 pts) List and explain the threads running in your Sorcerer's Cave program as reported by jconsole. Note that you can name the threads created in the program, as is done on line 6 in Problem 1 above, which can make this discussion a lot easier to follow.
5. (20 pts) Explain how the java.util.concurrent.Semaphore might class be used in the Sorcerer's Cave program, final project, to coordinate the requirements of the various jobs. Then address the question of whether or not this actually makes sense in the context of the requirements of program. In other words, can you suggest approaches to handling shared resource pools that would be simpler than using semaphores?
Explanation / Answer
In the Sorcerer’s cave program, the java.util.concurrent.Semaphore can be used to acquire block on the job thread until the job is performed or requirement gets fulfilled. The Semaphore class is used to maintain the permits, blocking and releasing the threads. So we can control the various job by their respective threads using this class by blocking and releasing threads of the job as per the requirement
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.