国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

重學(xué)JAVA基礎(chǔ)(四):線程的創(chuàng)建與執(zhí)行

2019-11-14 23:37:50
字體:
供稿:網(wǎng)友
重學(xué)java基礎(chǔ)(四):線程的創(chuàng)建與執(zhí)行

1.繼承Thread

  

public class TestThread extends Thread{    public void run(){        System.out.PRintln(Thread.currentThread().getName());    }        public static void main(String[] args) {        Thread t = new TestThread();        t.start();    }}

2.實(shí)現(xiàn)Runnable

public class TestRunnable implements Runnable{    @Override    public void run() {        System.out.println(Thread.currentThread().getName());            }    public static void main(String[] args) {        Thread t = new Thread(new TestRunnable());        t.start();    }}

3.線程池

public class TestThreadPool {        public static ExecutorService singlePool = Executors.newSingleThreadExecutor();        private static ExecutorService fixedPool = Executors.newFixedThreadPool(2);        private static ExecutorService cachedPool = Executors.newCachedThreadPool();        private static ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DefaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());    private static class DefaultThreadFactory implements ThreadFactory{        @Override        public Thread newThread(Runnable r) {            Thread t = new Thread(r);            return t;        }    }        public static void main(String[] args) {        singlePool.execute(new TestThreadP());        fixedPool.execute(new TestThreadP());        cachedPool.execute(new TestThreadP());        tpe.execute(new TestThreadP());    }        }class TestThreadP implements Runnable{    @Override    public void run() {        System.out.println(Thread.currentThread().getName());    }    }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 融水| 深水埗区| 西峡县| 岢岚县| 合作市| 兴海县| 镇沅| 清水县| 固镇县| 静乐县| 沁源县| 芒康县| 嘉祥县| 外汇| 平江县| 绥滨县| 石屏县| 高唐县| 莎车县| 芮城县| 黎川县| 嵩明县| 东辽县| 中山市| 务川| 温宿县| 景泰县| 五河县| 历史| 炉霍县| 广宁县| 淳安县| 综艺| 繁昌县| 上饶县| 尉氏县| 即墨市| 安龙县| 曲松县| 济源市| 大安市|