线程及开启方式

线程及开启方式

线程

进程:正在执行的程序
线程:具有完成独立任务的一条执行路径
多线程:一个程序拥有多条线程

多线程的好处:

  1. 可以提高进程和CPU的使用率
  2. 能够让多个程序看起来像同时执行
  3. 防止单线程出现阻塞
  4. 用于处理耗时任务

题外话:关于并发和并行(以后会详写)

  • 并发:在一段时间间隔内处理多个事务的能力;
  • 并行:在某一时刻,同时处理多个事务的能力;

方式一(继承Thread类)

注意:Thread类是一个普通类不是接口;
要想开启某一类的线程时,继承Thread类,并实现run()方法,在主方法中调用start()方法即可;

public static void main(String[] args) {
		MyThread my1 = new MyThread("线程1");
		MyThread my2 = new MyThread("线程2");
    	//开启线程1
		my1.start();
    	//开启线程2
		my2.start();		
	}


class MyThread extends Thread{
	
	public MyThread() {
	}
	public MyThread(String name) {
		super(name);
	}
	// Thread.currentThread()表示当前正在执行的是哪一条线程那么就是对应的那个线程的名称
	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
		}
	}
}

其结果:线程1和线程2在随机交替打印,原因是因为Cup在执行时是执行的原子性语句,并且随机执行线程。

方式二(实现Runnable接口)

Runnable接口中只有一个抽象方法run();子类实现run()方法即可,不用再继承Thread类。
因为Runnable接口中只有一个抽象方法,即该接口属于函数式接口@FunctionalInterface,可以用Lambda表达式(JDK1.8之后)

	public static void main(String[] args) {
		
		MyRunnable mr = new MyRunnable();
    	//作为参数出入Thread中,以调用start();
		Thread t = new Thread(mr);
    	//Lambda表达式,Thread类也实现了Runnable接口
		new Thread(() -> {
			for (int i = 0; i < 100; i++) {
				System.out.println("Lambda表达式" + i);
			}
		}).start();
		t.start();	
	}


class MyRunnable implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("Runnable接口" + i);
		}
	}

方式三(实现Callable接口)

Callable<V>接口与上面两种不同,他是有返回值的;
V call() throws Exception;

一般会和Future接口一起使用,借助FutureTask类来执行,FutureTask同时实现了FutureRunnable接口。

	public static void main(String[] args) throws Exception {
		Callable<Integer> c = new MyCallable();
   		FutureTask<Integer> fu =new FutureTask<Integer>(c);
		new Thread(fu).start();
        //通过FutureTask中的get()方法来获取call()的返回值
        System.out.println(fu.get());
        //可以通过匿名内部类来实现Callable接口
        Callable<Integer> cn = new Callable<Integer>(){
			int sum = 0;
			@Override
			public Integer call() throws Exception {
				for(int i = 1;i<100;i++){
					sum+=i;
					System.out.println(Thread.currentThread().getName()+"实现Callable线程"+sum);
				}
				return sum;
			}	
		};      
	}


class MyCallable implements Callable<Integer>{
	int sum = 0;
	@Override
	public Integer call() throws Exception {
		for(int i = 1;i<100;i++){
			sum+=i;
			System.out.println(Thread.currentThread().getName()+"实现Callable线程"+sum);
		}
		return sum;
	}
	
}

和线程池一起使用:(注意返回值是Future,使用FutureTask接收就要向下转型)

	public static void main(String[] args) throws Exception {
		ExecutorService pool = Executors.newCachedThreadPool();
        Future<Integer> fu = pool.submit(new Callable<Integer>(){
				int sum = 0;
				@Override
				public Integer call() throws Exception {
					for(int i = 1;i<100;i++){
						sum+=i;
						System.out.println(Thread.currentThread().getName()+"实现匿名Callable线程"+sum);
					}
					return sum;
				}
			});
        new Thread((FutureTask<Integer>)fu).start();
        System.out.println(fu.get());
        
        
      

以上
@Fzxey

原文地址:https://www.cnblogs.com/fzxey/p/10859318.html