yield
对线程调度器的一种建议: 我已经执行完重要部分了,可以切换给其他任务执行一段时间。
import java.util.concurrent.*;
class LiftOff implements Runnable {
protected int countDown =10;
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff() {};
public LiftOff(int countDown) {
this.countDown = countDown;
}
private String status() {
return "#" + id + "(" + (countDown>0? countDown: "LiftOff!") + "),";
}
public void run() {
while(countDown-->0){
System.out.print(status());
Thread.yield();
}
}
}
public class ThreadDemo {
public static void main(String... args) {
for(int i=0; i<5; i++) {
Thread t = new Thread(new LiftOff());
t.start();
}
}
}
#0(9),#3(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(LiftOff!),#2(9),#1(9),#3(8),#3(7),#3(6),#3(5),#3(4),#3(3),#3(2),#3(1),#3(LiftOff!),#2(8),#1(8),#2(7),#1(7),#2(6),#1(6),#2(5),#1(5),#2(4),#2(3),#2(2),#1(4),#1(3),#1(2),#1(1),#1(LiftOff!),#2(1),#2(LiftOff!),#4(9),#4(8),#4(7),#4(6),#4(5),#4(4),#4(3),#4(2),#4(1),#4(LiftOff!),
sleep
import java.util.concurrent.*;
class LiftOff implements Runnable {
protected int countDown =10;
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff() {};
public LiftOff(int countDown) {
this.countDown = countDown;
}
private String status() {
return "#" + id + "(" + (countDown>0? countDown: "LiftOff!") + "),";
}
public void run() {
while(countDown-->0){
try{
System.out.print(status());
TimeUnit.MILLISECONDS.sleep(100);
} catch(InterruptedException e) {
System.err.println("Interrupted");
}
}
}
}
public class ThreadDemo {
public static void main(String... args) {
for(int i=0; i<5; i++) {
Thread t = new Thread(new LiftOff());
t.start();
}
}
}
#1(9),#0(9),#2(9),#4(9),#3(9),#0(8),#3(8),#2(8),#1(8),#4(8),#0(7),#4(7),#3(7),#1(7),#2(7),#0(6),#3(6),#1(6),#4(6),#2(6),#0(5),#1(5),#4(5),#3(5),#2(5),#0(4),#1(4),#3(4),#4(4),#2(4),#0(3),#2(3),#4(3),#3(3),#1(3),#0(2),#4(2),#1(2),#3(2),#2(2),#0(1),#4(1),#3(1),#1(1),#2(1),#0(LiftOff!),#2(LiftOff!),#1(LiftOff!),#4(LiftOff!),#3(LiftOff!),
join
import java.util.concurrent.*;
class LiftOff implements Runnable {
protected int countDown =10;
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff() {};
public LiftOff(int countDown) {
this.countDown = countDown;
}
private String status() {
return "#" + id + "(" + (countDown>0? countDown: "LiftOff!") + "),";
}
public void run() {
while(countDown-->0){
System.out.print(status());
Thread.yield();
}
}
}
public class Joining {
public static void main(String... args) {
for(int i = 0; i<5; i++) {
try{
Thread t = new Thread(new LiftOff());
t.start();
t.join();
} catch(InterruptedException e) {
System.out.println("Interrupted!");
}
}
System.out.println("All threads completed");
}
}
#0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(LiftOff!),#1(9),#1(8),#1(7),#1(6),#1(5),#1(4),#1(3),#1(2),#1(1),#1(LiftOff!),#2(9),#2(8),#2(7),#2(6),#2(5),#2(4),#2(3),#2(2),#2(1),#2(LiftOff!),#3(9),#3(8),#3(7),#3(6),#3(5),#3(4),#3(3),#3(2),#3(1),#3(LiftOff!),#4(9),#4(8),#4(7),#4(6),#4(5),#4(4),#4(3),#4(2),#4(1),#4(LiftOff!),All threads completed