线程池

方便线程的创建;好的话使线程充分被利用 节省空间

  1. 使用线程时,需要频繁的创建和销毁线程。new Thread(Runnable a,String str); 是自己创建的线程 或者在线程类中直接继承线程类Thread;
  2. 线程池是创造好的几个线程在队列中排列; 用完还要放到队列的底部去重复利用

Executor接口

java.util.concurrent并发包下的一个接口
public interface Executor

Executors类

并发包下的一个类 public class Executors extends Object

  1. Executors.newCachedThreadPool(ThreadFactory thr) 无限个 程序中需要几个就创建几个
  2. Executors.newFixedThreadPool(int nThreads); 指定线程池中有几个线程
  3. Executors.newSingleThreadExecutor(); 就一个线程

适用情况

  1. 不适用于多个线程抢占一个资源的情况;啊当然也不适应于多个线程抢占多个资源 这都加锁了
  2. 适用于迅雷下片 多个线程执行相同的功能 一个线程执行一次功能 分配几个线程就会完成几遍这个功能
package 线程池;
//线程池中 维护了一个线程队列
 // 先进先出 后进后出  输出结果不能保证
/*
 * newCashedThreadPool(ThreadFactory thr) 无线个
 * newFIxedThreadPool(int Thread)   指定线程池中有几个线程
 * newSingleThreadPool()   次线程池中就一个线程
 * 
 * 
 * */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//使用线程池
		Haha haha=new Haha();//新建多线程的程序
		ExecutorService service=Executors.newFixedThreadPool(2);//不需要新建Thread线程
	
		    service.submit(haha);//service.submit(Runnable Haha)   给线程分配任务            重要: 一个线程只能完成一个对象所有的功能  不能多个线程同时干一个活  求0 到100 的偶数;创建一个对象(求偶数) submit()传递一次任务,只有创建线程中的一个来干活   分配几次对象就有几个线程来干活    使线程得到充分利用    不是多个线程干一个活干的快 
			service.submit(haha);//service.submit(Runnable Haha)   给线程分配任务                         多线程同步问题   干同一个活,五个线程相互影响  五个线程计算0到100的偶数  加锁抢苹果 枪的是5个苹果 两个线程
		    service.shutdown();//线程池使用完毕后要关闭
		
		

	}

}
class Haha implements Runnable{
	Lock lock =new ReentrantLock();

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for( int i=0;i<100;i++) {
			try {
				lock.lock();
				if(i%2==0) {
					
					System.out.println(Thread.currentThread().getName()+" "+i);
					
				}
			}finally {
				lock.unlock();
			}
			
			
		}
		
	}
	
	
	
}

分配两次资源 就是两个线程去干活 一个线程干一次

pool-1-thread-2 0
pool-1-thread-2 2
pool-1-thread-2 4
pool-1-thread-2 6
pool-1-thread-2 8
pool-1-thread-2 10
pool-1-thread-2 12
pool-1-thread-2 14
pool-1-thread-1 0
pool-1-thread-1 2
pool-1-thread-1 4
pool-1-thread-1 6
pool-1-thread-1 8
pool-1-thread-1 10
pool-1-thread-1 12
pool-1-thread-1 14
pool-1-thread-1 16
pool-1-thread-2 16
pool-1-thread-2 18
pool-1-thread-2 20
pool-1-thread-2 22
pool-1-thread-2 24
pool-1-thread-2 26
pool-1-thread-2 28
pool-1-thread-2 30
pool-1-thread-1 18
pool-1-thread-1 20
pool-1-thread-1 22
pool-1-thread-1 24
pool-1-thread-1 26
pool-1-thread-1 28
pool-1-thread-1 30
pool-1-thread-1 32
pool-1-thread-1 34
pool-1-thread-1 36
pool-1-thread-1 38
pool-1-thread-1 40
pool-1-thread-1 42
pool-1-thread-1 44
pool-1-thread-1 46
pool-1-thread-2 32
pool-1-thread-2 34
pool-1-thread-2 36
pool-1-thread-2 38
pool-1-thread-2 40
pool-1-thread-2 42
pool-1-thread-2 44
pool-1-thread-2 46
pool-1-thread-2 48
pool-1-thread-1 48
pool-1-thread-1 50
pool-1-thread-1 52
pool-1-thread-1 54
pool-1-thread-1 56
pool-1-thread-1 58
pool-1-thread-1 60
pool-1-thread-1 62
pool-1-thread-2 50
pool-1-thread-2 52
pool-1-thread-2 54
pool-1-thread-2 56
pool-1-thread-2 58
pool-1-thread-1 64
pool-1-thread-1 66
pool-1-thread-1 68
pool-1-thread-1 70
pool-1-thread-1 72
pool-1-thread-1 74
pool-1-thread-1 76
pool-1-thread-1 78
pool-1-thread-1 80
pool-1-thread-2 60
pool-1-thread-2 62
pool-1-thread-2 64
pool-1-thread-2 66
pool-1-thread-2 68
pool-1-thread-2 70
pool-1-thread-2 72
pool-1-thread-2 74
pool-1-thread-2 76
pool-1-thread-2 78
pool-1-thread-2 80
pool-1-thread-2 82
pool-1-thread-2 84
pool-1-thread-2 86
pool-1-thread-2 88
pool-1-thread-2 90
pool-1-thread-2 92
pool-1-thread-2 94
pool-1-thread-1 82
pool-1-thread-1 84
pool-1-thread-1 86
pool-1-thread-1 88
pool-1-thread-1 90
pool-1-thread-1 92
pool-1-thread-1 94
pool-1-thread-1 96
pool-1-thread-2 96
pool-1-thread-2 98
pool-1-thread-1 98
如果线程的个数小于分配任务的个数 就有线程干多了
如只有两个线程 却分配 3个任务;就会有一个线程多干一次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值