日期:2014-05-20 浏览次数:20650 次
public class Main { public static void main(String[] args) { for (int i = 0; i < 10; i++) { Runnable addit = new AddOne(); new Thread(addit).start(); } } } public class AddOne implements Runnable { static int cnt = 0; @Override public void run() { System.out.println(Thread.currentThread().getName() + ",before:" + cnt); cnt++; System.out.println(Thread.currentThread().getName() + ",after:" + cnt); } }
Thread-0,before:0 Thread-0,after:1 Thread-2,before:1 Thread-2,after:2 Thread-4,before:2 Thread-4,after:3 Thread-6,before:3 Thread-6,after:4 Thread-8,before:4 Thread-8,after:5 Thread-5,before:2 Thread-3,before:2 Thread-3,after:6 Thread-5,after:7 Thread-7,before:7 Thread-7,after:8 Thread-9,before:8 Thread-9,after:9 Thread-1,before:2 Thread-1,after:10