多线程基本概念

 

 

多线程

什么是程序:为了完成某项特定的任务,使用某种语言,编写一组指令的集合
什么是进程:是一个正在进行的程序
什么是线程:在一个进程中,执行的一套功能流程,称为线程

                        在一个进程中,执行的多套功能流程,称为多线程

多线程程序的优点

1、降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的消耗。

2、提高响应速度。当任务到达时,任务可以不需要的等到线程创建就能立即执行。

3、提高线程的可管理性。线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控。

但是当多个线程访问共享变量的时候 会出现线程安全的问题

线程的生命周期

 

声明线程的几种方法

一 、 创建一个类继承 Thread 类并且重写Thread 类的 run() 方法 

public class MyThread extends Thread{
	//线程执行体
	public void run() {
		for (int i = 0; i < 8; i++) {
			System.out.println(i);
		}
	}
}
public class TestMyThread {
	public static void main(String[] args) throws InterruptedException {
		//我创建的线程
		Thread myThread = new MyThread();
		//执行我的线程体
		myThread.start();

		//下面是主线程
		for (int i = 0; i < 8; i++) {
			Thread.sleep(1);
			System.out.println("haha");
		}
	}
}

打印结果

haha
haha
0
1
2
3
4
5
6
7
haha
haha
haha
haha
haha
haha
	public static void main(String[] args)throws Exception{
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i = 0;i<=3;i++) {
					try {
						Thread.sleep(2000);
						System.out.println(111111);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});

		//true 主线程结束 新开的线程也就结束
		//false 新线程没有结束 主线程不会结束
		//默认是false
		thread.setDaemon(false);
		thread.start();

	}
}

二、 创建一个类实现Runnable接口并且实现run方法

public class MyRunnable implements Runnable{
    private List<String> list;
    public MyRunnable (List<String> list){
        this.list = list;
    }
    //线程执行体
    @Override
    public void run(){
	//批量插入
        batchInsertDate(list);
    }
}
public class TestMyRunnable {
    public static void main(String[] args) {
        List<String>  list = new ArrayList<String>();
        //我创建的线程执行体
        MyThread myRunnable = new MyRunnable (list);
        //创建一个线程,并将我的线程执行体传入到线程
        Thread thread = new Thread(myRunnable,"线程的名称" );
        thread.start();
    }
}

三、 创建一个类实现Callable接口并且实现call方法(和Runnable不同的是Callable接口有返回值,Callable实现call方法,Callable接口有异常)启动Runnable接口用new Thread(Runnable runnable).start();

public class MyCallable implements Callable<List>{

	@Override
	public List call() throws Exception {
		ArrayList list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		System.out.println(list);
		return list;
	}
}
public class TestMyCallable {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		//new Thread(Runnable runnable)但是构造器中不能传入Callable接口
		//FutureTask实现了Runnable接口,可以将实现了Callable接口的类传进FutureTask的构造方法中
		FutureTask futureTask = new FutureTask(new MyCallable());
		new Thread(futureTask).start();

		//利用线程池执行Callable接口,可直接将Callable传给submit方法即可
		//该方法返回call方法的返回值对象Future<T> 并通过Future<T>.get()方法得到返回值
		ExecutorService excu = Executors.newFixedThreadPool(10);
		Future<List> returnValue = excu.submit(new MyCallable());

		System.out.println("返回值:" + returnValue.get());
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值