public class Test_sleep_yield_join {
public static void main(String[] args) {
testYield();
}
static void testSleep(){
new Thread(()->{
for(int i=0;i<100;i++){
System.out.println("sleep "+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
static void testYield(){
new Thread(()->{
for(int i=0;i<100;i++){
System.out.println("yield A "+i);
if(i%10==0){
Thread.yield();
}
}
}).start();
new Thread(()->{
for(int i=0;i<100;i++){
System.out.println("yield B "+i);
if(i%10==0){
Thread.yield();
}
}
}).start();
}
static void testJoin(){
Thread t1 = new Thread(()->{
for(int i=0;i<10;i++){
System.out.println("join t1 "+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(()->{
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<10;i++){
System.out.println("join t2 "+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}