1. (20 pts) For the following program, explain the interesting elements related
ID: 3829087 • Letter: 1
Question
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
2. (20 pts) What is changed if the method called on line 7, start(), is replaced with run()? Explain (of course).
3. (20 pts) What is changed if the method Thread.yield() is added between lines 23 and 24? Explain.
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
Hi, I have answered first 3 Questions.
Please repost other questions in separate post.
Please let me know in case of any issue.
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); // runnable object
6 Thread ts = new Thread (ps, s); // creating threa
7 ts.start (); // starting object
8 } // end for each character
9 } // end main
10 } // end class TaskThreadDemo
11
//THIS IS A RUNNABLE INTERFACE IMPLEMENTER
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 () { // THis is the method run by the thread
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
Here, at line 6, we are creating a new thread that target new PrintChar object. We are passing cureent string that
will be printed 200 times in PrintChar class.
An Instance of Thread is an object like any other object in Java, it contains variables and methods which lives and dies on the heap. But a Thread of Execution is an individual process which has its own stack call. Even if you don’t create any thread in your program, thread will be running there and main() method will start that.
Every thread of execution begins as an instance of class Thread. In any case Thread objects needs to be created before creating Thread of Execution.
you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of the run() method
public void run( )
Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method
2)
If the method called on line 7, start(), is replaced with run() then it is the normal method call on PrintChar object
It will not start a thread
3)
if the method Thread.yield() is added between lines 23 and 24 then current executing thread will be preempted from the CPU, and it
will be added in wainting queue.
yield: yield method is related with the priority of Thread. It is also static method so it works only on currently running thread. Scheduler make it sure that If a thread enters the runnable state, and it has a higher priority than any of the threads in the pool and a higher priority than the currently running thread, the lower-priority running thread usually will be bumped back to runnable and the highest-priority thread will be chosen to run. at any given time the currently running thread usually will not have a priority that is lower than any of the threads in the pool. yield() is supposed to do is make the currently running thread head back to runnable to allow other threads of the same priority to get their turn.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.