package Thread;
public class MyThread implements Runnable{
protected int countDown = 10;
private static int taskCount = 0;
private final int id = taskCount++;
public MyThread(){}
public MyThread(int countDown){
this.countDown = countDown;
}
public String status(){
return "#"+id+"("+(countDown>0?countDown+"),":"LiftOff"+")");
}
@Override
public void run() {
while(countDown-->0){
System.out.print(status());
<span style="color:#ff0000;">Thread.yield(); </span> //here
}
}
public static void main(String args[]){
MyThread mt = new MyThread();
mt.run();
}
}
output #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(LiftOff)
package Thread;
public class BasicThread implements Runnable{
private static int taskCount;
private final int id = taskCount++;
BasicThread(){
System.out.println("BasicThread started,ID = "+id);
}
@Override
public void run() {
System.out.println("Stage 1....,ID = "+id+" "+Thread.currentThread().getName());
<span style="color:#ff0000;">Thread.yield(); </span>
System.out.println("Stage 2....,ID = "+id+" "+Thread.currentThread().getName());
<span style="color:#ff0000;">Thread.yield();</span>
System.out.println("BasicThread ended, ID = "+id+" "+Thread.currentThread().getName());
}
public static void main(String args[]){
for(int i=0;i<5;i++){
Thread thread = new Thread(new BasicThread());
thread.start();
}
}
}
output::
BasicThread started,ID = 0
BasicThread started,ID = 1
Stage 1....,ID = 0 Thread-0
Stage 1....,ID = 1 Thread-1
BasicThread started,ID = 2
Stage 2....,ID = 0 Thread-0
Stage 2....,ID = 1 Thread-1
BasicThread started,ID = 3
BasicThread ended, ID = 1 Thread-1
BasicThread ended, ID = 0 Thread-0
Stage 1....,ID = 3 Thread-3
BasicThread started,ID = 4
Stage 2....,ID = 3 Thread-3
Stage 1....,ID = 4 Thread-4
Stage 2....,ID = 4 Thread-4
BasicThread ended, ID = 3 Thread-3
BasicThread ended, ID = 4 Thread-4
Stage 1....,ID = 2 Thread-2
Stage 2....,ID = 2 Thread-2
BasicThread ended, ID = 2 Thread-2