Java多线程你知道有几种实现方式?
Posted JunMain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程你知道有几种实现方式?相关的知识,希望对你有一定的参考价值。
多线程的四种实现
1. 继承Thread
1.创建一个MyThread 类继承Thread
2. 并且重写run()方法
pakage com.lzj;
public class MyThread extends Thread{
@Override
public void run(){
for (int i = 1; i < 20; i++){
System.out.println("我是 "+Thread.currentThread().getName()+"线程 "+i);
}
}
}
3.创建MyThread实例
4.调用start()方法启动线程
pakage com.lzj;
public class ThreadTest {
//Thread.currentThread()可以获取当前正在运行的线程 getName()获取线程名
@Test
public void testThread(){
MyThread myThread = new MyThread();
myThread.start();
for (int i = 0; i < 20; i++){
System.out.println("我是 "+Thread.currentThread().getName()+"线程 "+i);
}
}
}
2. 实现Runnable接口
1. 创建类实现Runnable接口
2. 实现run()方法
package com.lzj;
public class MyRunnable implements Runnable{
public void run(){
for (int i = 0; i < 40; i++){
System.out.println("我是 " + Thread.currentThread().getName()+"线程 "+i);
}
}
}
3. 创建MyRunnable实例
4. MyRunnable实例作为创建Thread类实例的参入传入
5. 调用start()
pakage com.lzj;
public class ThreadTest {
@Test
public void testRunnable(){
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable,"通过实现Runnable接口的");
thread.start();
for (int i = 0; i < 20; i++){
System.out.println("我是 "+Thread.currentThread().getName()+"线程 "+i);
}
}
}
3. 实现Callable接口
1.创建MyCallable实现Callable接口 传入泛型的类型作为call的返回值类型
2.实现call()方法
package com.lzj;
import java.util.concurrent.Callable;
public class MyCallable implements Callable<String> {
public String call() {
for(int i = 1; i < 30; i++){
System.out.println("我是"+Thread.currentThread().getName()+"线程"+i);
}
return "实现Callable接口的线程运行结束";
}
}
3.创建MyCallable实例 MyCallable myCallable = new MyCallable();
4.创建FutureTask的实例 myCallable 作为参数 泛型的类型与Callable泛型的类型一致
5.创建Thread的实例 futureTask作为参数传入
6.调用start()方法
7.可以futureTask.get()获取call()的返回值 捕获异常
pakage com.lzj;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadTest {
@Test
public void testCallable(){
MyCallable myCallable = new MyCallable();
FutureTask<String> futureTask = new FutureTask<String>(myCallable);
Thread thread = new Thread(futureTask,"通过实现Callable接口的");
thread.start();
for (int i = 1; i < 30; i ++){
System.out.println("我是 "+Thread.currentThread().getName()+"线程 "+i);
}
String result = null;
try {
result = futureTask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println(result);
}
}
4. 通过线程池创建
1.通过Executors这个包中提供的newFixedThreadPool()创建定长线程池
2.execute()执行线程 MyRunnable实体类作为参数
package com.lzj;
public class MyRunnable implements Runnable{
public void run(){
for (int i = 0; i < 40; i++){
System.out.println("我是 " + Thread.currentThread().getName()+"线程 "+i);
}
}
}
package com.lzj;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyThreadPoolTest {
@Test
public void testPool(){
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.execute(new MyRunnable());
for (int i = 0; i < 20; i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
以上是关于Java多线程你知道有几种实现方式?的主要内容,如果未能解决你的问题,请参考以下文章