Verified Answers


CLICK HERE TO DOWNLOAD THIS ANSWER  INSTANTLY $15 Only

 

Homework 4

  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);
7ts.start ();
8 } // end for each character
9} // end main
10} // end class TaskThreadDemo
11
12class PrintChar implements Runnable {
13String ch;
14int times;
15
16public PrintChar (String c, int n) {
17ch = c;
18times = n;
19} // end constructor
20
21public void run () {
22for (int i = 0; i < times; i++) {
23System.out.print (ch);
24} // end for loop
25} // end method run
26} // end class PrintChar

  1. (20 pts) What is changed if the method called on line 7, start(), is replaced with run()? Explain (of course).



  1. (20 pts) What is changed if the method Thread.yield() is added between lines 23 and 24? Explain.