4java多线程实现方式

来源:互联网 发布:淘宝十年产品事扫描 编辑:程序博客网 时间:2024/06/05 15:54

1.继承Thread

package com.b2bex.wrc.lsx;


public class MyTread1 {


public static void main(String[] args) {
System.out.println("主线程ID:" + Thread.currentThread().getId());
MyThread thread1 = new MyThread("thread1");
thread1.start();
MyThread thread2 = new MyThread("thread2");
thread2.run();
}
}


class MyThread extends Thread {
private String name;


public MyThread(String name) {
this.name = name;
}


@Override
public void run() {
System.out.println("name:" + name + " 子线程ID:"+ Thread.currentThread().getId());
System.out.println("name:" + name + " 子线程Name:"+ Thread.currentThread().getName());
}
}

2、实现Runnable接口

package com.b2bex.wrc.lsx;


public class MyThread2 {
    public static void main(String[] args) {
    MyRunnable myRunnable=new MyRunnable();
    Thread thred1=new Thread(myRunnable);
    thred1.start();
}
}
class MyRunnable implements Runnable{


@Override
public void run() {
System.out.println(" 子线程ID:"+Thread.currentThread().getId());
}

}


3、实现Callable接口,通过futuretask封装

package com.b2bex.wrc.lsx;


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


public class Test1 {
public static void main(String[] args) {
MyCallable1 myCallable1=new MyCallable1("线程");
FutureTask<Object>  futureTask=new FutureTask<Object>(myCallable1);
Thread oneThread = new Thread(futureTask);   
oneThread.start();
try {
System.out.println(futureTask.get().toString());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 


}
class MyCallable1 implements Callable{
private String name;
public MyCallable1(String name){
this.name=name;
}
@Override
public Object call() throws Exception {
String returnString="name:" + name + " 子线程ID:"+ Thread.currentThread().getId();
return returnString;
}

}


4、通过线程池Executors实现

package com.b2bex.wrc.lsx;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class Test {
public static void main(String[] args) throws ExecutionException,InterruptedException {
System.out.println("----程序开始运行----");
Date date1 = new Date();


int taskSize = 5;
// 创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
// 创建多个有返回值的任务
List<Future> list = new ArrayList<Future>();
for (int i = 0; i < taskSize; i++) {
Callable c = new MyCallable(i + " ");
// 执行任务并获取Future对象
Future f = pool.submit(c);
// System.out.println(">>>" + f.get().toString());
list.add(f);
}
// 关闭线程池
pool.shutdown();


// 获取所有并发任务的运行结果
for (Future f : list) {
// 从Future对象上获取任务的返回值,并输出到控制台
System.out.println(">>>" + f.get().toString());
}


Date date2 = new Date();
System.out.println("----程序结束运行----,程序运行时间【"+ (date2.getTime() - date1.getTime()) + "毫秒】");
}
}


class MyCallable implements Callable<Object> {
private String taskNum;


MyCallable(String taskNum) {
this.taskNum = taskNum;
}


public Object call() throws Exception {
System.out.println(">>>" + taskNum + "任务启动");
Date dateTmp1 = new Date();
Thread.sleep(1000);
Date dateTmp2 = new Date();
long time = dateTmp2.getTime() - dateTmp1.getTime();
System.out.println(">>>" + taskNum + "任务终止");
return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
}
}


原创粉丝点击