java线程

参考大神作品:https://www.cnblogs.com/3s540/p/7172146.html

java线程创建的四种方式

1、继承Thread类,重写Thread类中的run()方法

public class TestThread extends Thread{
	public void run(){
		System.out.println("线程运行");
	}
}

class Test{
	public static void main(String[] args) {
		new TestThread().start();
	}
}

2、实现Runnable接口,重写run()方法
这里要注意的是,在创建线程对象之前,要先创建实现类的对象,然后将这个实现类的对象作为Thread的target来使用

public class TestThread implements Runnable{
	public void run(){
		System.out.println("线程运行Runnable");
	}
}

class Test{
	public static void main(String[] args) {
		TestThread myThread = new TestThread();
		Thread t = new Thread(myThread);
		t.start();
	}
}

3、实现callable接口,重写call()方法,配合futureTask和线程池的submit()方法实现任务执行

public class TestThread implements Callable<Integer>{

	@Override
	public Integer call() throws Exception {
		System.out.println("子线程执行任务");
		Thread.sleep(3000);
		return 1000;
	}
}

class Test{
	public static void main(String[] args) {
		ExecutorService executor = Executors.newCachedThreadPool();
		TestThread t = new TestThread();
		FutureTask<Integer> futureTask = new FutureTask<Integer>(t);
		//executor.execute(futureTask);
		executor.submit(futureTask);
		executor.shutdown();
		
		System.out.println("主线程执行任务");

		try {
			System.out.println("运行结果:"+futureTask.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}

4、实现callable接口,重写call()方法,配合future和线程池的submit()方法实现任务执行

class Test{
	public static void main(String[] args) {
		ExecutorService executor = Executors.newCachedThreadPool();
		TestThread t = new TestThread();
		Future<Integer> future = executor.submit(t);
		//executor.execute(futureTask);
		executor.shutdown();
		
		System.out.println("主线程执行任务");

		try {
			System.out.println("运行结果:"+future.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}

java的几种线程池

1、newCacheThreadPool
池子创建之初并没有线程,当使用了submit方法或者excute方法提交任务时,会将该任务插入到同步队列(SynchronousQueue),然后在池中寻找可用的工作线程来执行任务,若有则直接使用,若没有则创建,这种线程池一般可容纳几万个线程,但是里面的空闲线程在一定的时间内没有任务执行,会被销毁

2、newFixedThreadPool
这是个定长的池子,在创建之初需要参数n来固定池子的容量,使用的是无解阻塞队列(LinkedBlockingQueue() ),如果线程池中的工作线程达到最大容量,则将任务加入队列等待

3、newSingleThreadPool
池子中永远只有一个线程,如果扔五个任务进来,则会有四个处于等待状态,使用的是无解阻塞队列(LinkedBlockingQueue() )

4、newScheduleThreadPool
这是一种定时器线程池,可以周期性执行任务,也是需要一个参数n来创建一个固定大小的线程池,底层使用的是new DelayedWorkQueue() ,一个按超时时间升序排序的队列


线程池执行任务的流程:
1、当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。
2、当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行
3、当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务
4、当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理
5、当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程
6、当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值