- class Printer implements Runnable {
- private static int taskCount;
- private final int id = taskCount++;
- Printer() {
- System.out.println("Printer started, ID = " + id);
- }
- public void run() {
- System.out.println("Stage 1..., ID = " + id); 这里的id应该如何理解啊??final型不是只能赋一次值嘛,怎么结果会显示0,1,2,3,??
- Thread.yield();
- System.out.println("Stage 2..., ID = " + id);
- Thread.yield();
- System.out.println("Stage 3..., ID = " + id);
- Thread.yield();
- System.out.println("Printer ended, ID = " + id);
- }
- }
-
- public class E01_Runnable {
- public static void main(String[] args) {
- for(int i = 1; i < 5; i++)
- new Thread(new Printer()).start();
- }
- }
复制代码 |