2. What is changed if the method called on line 7, start(), is replaced with run
ID: 3798982 • Letter: 2
Question
2. What is changed if the method called on line 7, start(), is replaced with run()? Explain (of course).
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
Explanation / Answer
Now Coming to the above problem,
The above program calls the threads using start() method which executes the task in seperate call stack.So, each thread will not depend on others. Each thread executes in it's own call stack.
So, we will get the output of multiple threads at the same time in the console.
Instead, if we call the above program by using the run() method, The thread will execute ib the current stack where main() is running. All the threads will execute in the same stack . so , for running a new thread, it should wait for the current thread to complete execution. so, All the threads will not execute at the same time but one by one.
Program 1:- by using start()
public class TaskThreadDemo {
public static void main (String args []) {
String [] sa = {"a", "X", "+", "."};
for (String s: sa) {
Runnable ps = new PrintChar (s, 200);
Thread ts = new Thread (ps, s);
ts.start ();
} // end for each character
} // end main
} // end class TaskThreadDemo
class PrintChar implements Runnable {
String ch;
int times;
public PrintChar (String c, int n) {
ch = c;
times = n;
} // end constructor
public void run () {
for (int i = 0; i < times; i++) {
System.out.print (ch);
} // end for loop
} // end method run
} // end class PrintChar
output :-
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+++++++++++++++++++++++++++++++++++++++++aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.................................................................................+++++++++++++++.......................................................................................................................
Program 2 :- by using run():-
public class TaskThreadDemo {
public static void main (String args []) {
String [] sa = {"a", "X", "+", "."};
for (String s: sa) {
Runnable ps = new PrintChar (s, 200);
Thread ts = new Thread (ps, s);
ts.run();
} // end for each character
} // end main
} // end class TaskThreadDemo
class PrintChar implements Runnable {
String ch;
int times;
public PrintChar (String c, int n) {
ch = c;
times = n;
} // end constructor
public void run () {
for (int i = 0; i < times; i++) {
System.out.print (ch);
} // end for loop
} // end method run
} // end class PrintChar
Output:-
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........................................................................................................................................................................................................
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.