美文网首页
007--多线程

007--多线程

作者: 糖纸疯了 | 来源:发表于2018-04-26 14:06 被阅读6次

    第一个小例子

    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");
        }
    }
    

    相关文章

      网友评论

          本文标题:007--多线程

          本文链接:https://www.haomeiwen.com/subject/gfqwfftx.html