欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java需要关注的知识点---并发之定义任务

程序员文章站 2022-03-23 14:57:57
...
1:定义任务:

public class LiftOff implements Runnable{
private int count = 10;
private static int taskCount = 0;
private final int id = taskCount++;
public void run() {
while(count-- > 0) {
System.out.println(status());
Thread.yield();
}
}
public String status() {
return "#" + id + "(" + (count >0 ? count:"Liftoff")+ "),";
}
public static void main(String[] args) {
LiftOff lift = new LiftOff();
new Thread(lift).start();
}
}




public class BasicThreads {

/**
* @param args
*/
public static void main(String[] args) {
Thread t= new Thread(new LiftOff());
t.start();
System.out.println("Waiting for LiftOff");
}

}


public class MoreBasicThread {

/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i< 5; i++) {
new Thread(new LiftOff()).start();
}
System.out.println("Waiting for liftOff");
}

}