1. For the following program, explain the interesting elements related to thread
ID: 3767174 • Letter: 1
Question
1. For the following program, explain the interesting elements related to threads.
You may experiment with the program using the attached source code: TaskThreadDemo source code
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. What is changed if the method called on line 7, start(), is replaced with run()? Explain (of course).
3. What is changed if the method Thread.yield() is added between lines 23 and 24? Explain.
4. 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. Explain how the java.util.concurrent.Semaphore class can be used in the Sorcerer's Cave program, final project, to coordinate the requirements of the various jobs.
Explanation / Answer
Answers:
1)
The output generated is combination of the string array values i.e., “a, X, +, .” . The thread class here implements the Runnable interface where it creates a new Thread executes a piece of code using the multithreading concepts. So that it executes the program parallel with respect to other code.
2)
In code at line 7, if the start() method is invoked, it creates a new thread and executes the run() method of the runnable interface. The run() method contains the logic to execute the program.
The output generated by the start() method is as follows:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXXXXXXXXXXaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........................................................................................................................................................................................................XXXXXXXXXXXX
If the code at line 7 is changed from start() to run() method then it first executes the first thread that means it does not create a new thread, it executes the run() method with in the created thread itself.
So, first it finishes the printing the values of “a” and then with next index value of the string array and then next and so on.
First it finishes the first thread and then it executes the second thread.
The output generated by the run() method in the program is as follows:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........................................................................................................................................................................................................
3)
If the Thread.yield() method is placed in-between the line 23 and line 24 with start() method at line 7 then it makes the current thread to pause for some time and gives the priority for the other threads to execute. At times it gives equal priority to other threads too. If there are no threads with the least priority value then it allows the current thread to continue its execution.
When the Thread.yield() method is placed in-between the lines 23 and line 24, the following output is generated shows the working priority of the thread by using yield functionality.
The output generated is,
a...............................................................................................................XaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.........................................................................................+X+X+X+X+X+X++++++++++++++++++++++++++++++++++++++++++++++++++++++++XXXXXXXXXXXXX++++++++++++++++++++++++XXXXX++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+X+X+X+X+X+X+X++X+X+X+XXXXXXXXXX++++++++++++++++++
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.