关于Java线程池的一点理解



MyThread 写道

package com.honzh.mwq.test; 


import java.io.IOException; 


public class MyThread implements Runnable { 

private int i; 


public MyThread(int i) { 

this.i = i; 


public void run() { 

try { 

FileOutUtil.out(Thread.currentThread().getName() + " " + this.i + " 正在执行..."); 

} catch (IOException e) { 

e.printStackTrace(); 

}

public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        for (int i = 0 ; i < 100000000; i++) {
            pool.execute(new MyThread(i));
        }
 
        pool.shutdown();
        try {
            FileOutUtil.out("pool is shutdown");
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }


public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0 ; i < 1000000000; i++) {
            pool.execute(new MyThread(i));
        }
 
        pool.shutdown();
        try {
            FileOutUtil.out("pool is shutdown");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



上一篇:推荐使用序列化实现对象的拷贝


下一篇:大量文件名记录的树形结构存储