Thanks to all for replying me.
There is one another quetion is asked like this that is
Sample Code
1: import java.io.*;
2: class PrintThread extends
Thread {
3: private static Object o = new Object();
4: private static DataOutputStream out = //... set to some output file
5: private int I;
6:
7: public PrintThread(int I) {
8: this.I = I;
9: }
10: public void run() {
11: while(true) {
12: try {
13: out.writeBytes("Hello all from thread " + I +"\r\n");
14: } catch (Exception e) {}
15: }
16: }
17: public static void main(
String[] s) {
18: for (int i = 0; i < 5; i++ ) {
19: (new PrintThread(i)).start();
20: }
21: }
22: }
How could you ensure that each line of output from the above program came from an individual thread?
Choice 1
Add the "synchronized" modifier to the DataOuputStream "out" variable declaration.
Choice 2
Insert the following line between line 12 and 13:
synchronized(o) {
and this line between line 13 and 14
}
Choice 3
Insert the following code between line 11 and 12:
yield();
Choice 4
Insert the following code between line 11 and 12:
try {
sleep(100);
} catch(Exception e) {}
Choice 5
Make the run method "synchronized".