Java的并发编程:创建线程的多种方式

[

这篇文章主要讲的是Java的并发编程:创建线程的多种方式,如果不了解线程之间的各个状态的话,可以查看:Java的并发编程:线程状态以及各个线程状态的关系这篇文章。
Java的并发编程:创建线程的多种方式有下面这几种方式:

  • 继承Thread类
  • 实现Runnable接口
  • 匿名内部类的方式
  • 带返回值的线程
  • 定时器(quartz)
  • 线程池的实现
  • Lambda表达式实现
  • Spring实现多线程

继承Thread类创建Java线程

package com.breakyizhan.thread.t2;

public class Demo1 extends Thread {
	
	public Demo1(String name) {
		super(name);
	}
	
	@Override
	public void run() {
		while(!interrupted()) {
			System.out.println(getName() + "线程执行了 .. ");
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		
		Demo1 d1 = new Demo1("first-thread");
		Demo1 d2 = new Demo1("second-thread");
		
		
		d1.start();
		d2.start();
		
//		d1.stop();
		d1.interrupt();
	}
	
}

实现Runnable接口创建Java线程

package com.breakyizhan.thread.t2;

/**
 * 作为线程任务存在
 * 
 * @author worker
 *
 */
public class Demo2 implements Runnable {

	@Override
	public void run() {
		while(true) {
			System.out.println("thread running ...");
		}
	}

	public static void main(String[] args) {
		Thread thread = new Thread(new Demo2());
		thread.start();
	}
	
}

匿名内部类的方式创建Java线程

package com.breakyizhan.thread.t2;

public class Demo3 {
	
	public static void main(String[] args) {
		
		/*new Thread() {
			public void run() {
				System.out.println("thread start ..");
			};
		}.start();*/
		
		
		/*new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("thread start ..");
			}
		}).start();*/
		
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("runnable");
			}
		}) {
			public void run() {
				System.out.println("sub");
			};
		}.start();
		
		
	}

}

带返回值的线程

package com.breakyizhan.thread.t2;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Demo4 implements Callable<Integer> {
	
	
	public static void main(String[] args) throws Exception {
		Demo4 d = new Demo4();
		
		FutureTask<Integer> task = new FutureTask<>(d);
		
		Thread t = new Thread(task);
		
		t.start();
		
		System.out.println("我先干点别的。。。");
		
		Integer result = task.get();
		System.out.println("线程执行的结果为:" + result);
	}

	@Override
	public Integer call() throws Exception {
		System.out.println("正在进行紧张的计算....");
		Thread.sleep(3000);
		return 1;
	}

}

定时器(quartz)创建Java线程

package com.breakyizhan.thread.t2;

import java.util.Timer;
import java.util.TimerTask;

public class Demo5 {

	public static void main(String[] args) {

		Timer timer = new Timer();

		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				// 实现定时任务
				System.out.println("timertask is run");
			}
		}, 0, 1000);

	}

}

线程池实现创建Java线程

package com.breakyizhan.thread.t2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo6 {

	public static void main(String[] args) {

		ExecutorService threadPool = Executors.newCachedThreadPool();

		for (int i = 0; i < 1000; i++) {
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName());
				}
			});
		}
		
		threadPool.shutdown();
	}

}

Lambda表达式实现创建Java线程

package com.breakyizhan.thread.t2;

import java.util.Arrays;
import java.util.List;

public class Demo7 {
	
	public static void main(String[] args) {
		
		List<Integer> values = Arrays.asList(10,20,30,40);
		int res = new Demo7().add(values);
		System.out.println("计算的结果为:" + res);
		
		
	}
	
	
	
	public int add (List<Integer> values) {
//		values.parallelStream().forEach(System.out :: println);
		return values.parallelStream().mapToInt( i -> i * 2).sum();
	}

}

Spring实现多线程创建Java线程在以后可以继续了解一下。

]
转载请保留页面地址:https://www.breakyizhan.com/java/6615.html

posted on 2020-07-05 11:03  MrAit  阅读(200)  评论(0编辑  收藏  举报

导航