I want to make the print method to do both system.out.print statements at the same time, i.e I want the console to print all the time:
Thread-0 playing:
Thread-0 stop playing
Thread-1 playing:
Thread-1 stop playing
instead of:
Thread-0 playing:
Thread-1 playing:
Thread-0 stop playing
Thread-1 stop playing
public class Program {
static class Player implements Runnable{
public void print(){
synchronized (this){
System.out.println(Thread.currentThread().getName() + " playing: ");
System.out.println(Thread.currentThread().getName() + " stop playing");
}
}
@Override
public void run() {
try { Thread.sleep(50); } catch(InterruptedException e) { System.out.println(e); }
print();
}
}
public static void main(String[] args) {
int numberOfPlayers = 2;
for (int i = 1; i<=numberOfPlayers; i++ ) {
Thread thread = new Thread(new Player());
thread.start();
}
}
}