In the code below how can I make number printer in black and red in order we expect, like below:
- 1 (black)
1 (red)
2 (black)
2 (red)
as opposed to what it actually does(some random order depending on whether the err or out streams are available):
- 1 (black)
2 (black)
1 (red)
2 (red)
code:
package threads;
public class CThread implements Runnable
{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 20; i++) {
outt(i);
err(i);
}
}
synchronized public void outt(int i){
System.out.println(i);
}
synchronized public void err(int i){
System.err.println(i);
}
public static void main(String [] org){
Thread th = new Thread(new CThread());
th.start();
}
}