第一个小例子
public class TestRunable {
public static void main(String[] args) throws InterruptedException {
TestRunable aRunable = new TestRunable();
aRunable.testOne();
aRunable.testTwo();
aRunable.testThree();
}
private void testOne() throws InterruptedException {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
Thread.sleep(index * 1000);
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(index);
}
});
}
}
private void testTwo() {
System.out.println("222222222222222");
}
private void testThree() {
System.out.println("333333333333333");
}
}
网友评论