线程创建的方式
1、继承Thread 进行实现(1种写法)
2、实现Runnable 接口进行实现(四种写法)
3、实现Callable接口进行实现(两种写法)(带返回值)
多线程的优势
执行效率大大提高了
JDK8以上的版本推荐使用方法四来创建线程
需要带返回值的推荐使用方法7
方法一继承Thread实现
/*
继承Thread
*/
public static void main(String[] args) {
//获得当前线程
Thread mianThread = Thread.currentThread();
System.out.println("线程名称"+mianThread.getName());
Thread thread = new MyThread();
//开启线程
thread.start();
}
static class MyThread extends Thread{
public void run(){
//业务代码
Thread thread = Thread.currentThread();
try {
Thread.sleep(1000);
}catch (InterruptedException e ){
e.printStackTrace();
}
System.out.println("线程名称:"+thread.getName());
}
}
方法2实现Runnable接口新建线程
public static void main(String[] args) {
//创建一个线程
MyThread2 myThread2 = new MyThread2();
Thread thread = new Thread(myThread2);
//启动线程
thread.start();
}
static class MyThread2 implements Runnable{
@Override
public void run() {
Thread thread = Thread.currentThread();//得到当前线程
System.out.println("执行线程"+thread.getName());
}
}
方法3Runnable匿名内部类来创建
/**
* Runnable匿名内部类来创建
*/
public class ThreadDemo2 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Thread t = Thread.currentThread();
System.out.println("执行名称"+t.getName());
}
});
thread.start();
}
方法4使用lambda表达式来创建Runnable
/**
* 使用lambda表达式来创建Runnable
*/
public class ThreadDemo3 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
//具体的业务
Thread t = Thread.currentThread();
System.out.println("线程名称"+t.getName());
});
//启动线程
thread.start();
}
}
方法5创建线程并初始化
/**
* 创建线程并初始化
*/
public class ThreadDemo4 {
public static void main(String[] args) {
Thread thread = new Thread(){
@Override
public void run() {
Thread t = Thread.currentThread();
System.out.println("线程名称"+t.getName());
}
};
thread.start();
}
}
方法6失实现Callable创建新线程
/**
*失实现Callable创建新线程
*/
public class ThraedDemo5 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建Callbale实例
MyCallable myCallable = new MyCallable();
//FutureTask用于接收Callable 结果的对象
FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
//创建新新线程
Thread thread = new Thread(futureTask);
//启动线程
thread.start();
int resul = futureTask.get();
System.out.println(Thread.currentThread().getName() +
"新线程返回的结果为:" + resul);
}
}
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
//生成随机数0-9
int randomNum = new Random().nextInt(11);
System.out.println(Thread.currentThread().getName()+
"--随机数"+randomNum);
return randomNum;
}
}
方法7
FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
//新线程执行的业务代码
String[] arrs = new String[]{"java","mysql","Thread"};
//随机返回一个字符串
String resul = arrs[new Random().nextInt(3)];
System.out.println(Thread.currentThread().getName() +
"--字符串" + resul);
return resul;
}
});
Thread thread = new Thread(futureTask);
//启动线程
thread.start();
String result = futureTask.get();
System.out.println(Thread.currentThread().getName() +
"--新线程的返回值" + result);
}