方法一:继承Thread
Thread
package com.bjsxt.create;
/**
* @author dell
* @data 2021/3/2
* 创建一个线程对象,并启动线程
*
* 注意:启动main方法,自动创建main线程
*
* thread.join() 阻塞乌龟线程,乌龟执行完,兔子才有机会
*
* Thread类的常用方法:
* public void run()
* thread.start();
* this.getName()
* this.getPriority()
* thread.setNam()
* Thread.currentThread().getPriority()
*
*/
public class TextThread {
public static void main(String[] args) throws InterruptedException {
//启动乌龟线程
Thread thread = new TortoiseThread();
thread.setName("乌龟1线程");
thread.start(); //启动一个新的线程
thread.join(); //阻塞乌龟1线程,其他执行完,乌龟1才有机会
Thread thread2 = new TortoiseThread();
thread2.setName("乌龟2线程");
thread2.start(); //启动一个新的线程
//兔子也在跑
Thread.currentThread().setName("主线程");
while (true){
System.out.println("兔子领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
}
}
}
main
package com.bjsxt.create;
/**
* @author dell
* @data 2021/3/2
* 定义一个乌龟线程类
*/
public class TortoiseThread extends Thread{
/**
* 线程体:线程要执行的任务
*/
@Override
public void run() {
while(true){
System.out.println("乌龟领先" +this.getName()+ " "+this.getPriority());
}
}
}
方法二:实现Runnable
Runnable
package com.bjsxt.create2;
/**
* @author dell
* @data 2021/3/2
* 定义线程方式2: 实现Runnable接口
* 创建线程对象:先定义一个任务,Runnable runnable = new TortoiseRunnable();
* 再创建一个线程,Thread thread1 = new Thread(runnable);
*
* •两种方式的优缺点
* 方式1:继承Thread类
* 缺点:Java单继承,无法继承其他类
* 优点:代码稍微简单
* 方式2:实现Runnable接口
* 优点 还可以去继承其他类 便于多个线程共享同一个资源
* 缺点:代码略有繁琐
* 实际开发中,方式2使用更多一些
*
*/
public class TortoiseRunnable implements Runnable {
@Override
public void run() {
while (true){
System.out.println("乌龟领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
}
}
}
main
package com.bjsxt.create2;
/**
* @author dell
* @data 2021/3/2
*/
public class TextThread {
public static void main(String[] args) {
//两个乌龟在跑
Runnable runnable = new TortoiseRunnable();
Thread thread1 = new Thread(runnable);
thread1.setName("乌龟1线程");
thread1.setPriority(Thread.MAX_PRIORITY);
thread1.start();
//Runnable runnable1 = new TortoiseRunnable();
Thread thread2 = new Thread(runnable,"乌龟2线程");
thread2.start();
//一个兔子在跑
Thread.currentThread().setName("主线程");
while (true){
System.out.println("兔子领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
}
}
}
优化:使用匿名内部类来创建线程对象
package com.bjsxt.create2;
/**
* @author dell
* @data 2021/3/2
* •可以使用匿名内部类来创建线程对象
*/
public class TextThread2 {
public static void main(String[] args) {
//两个乌龟在跑
Runnable runnable = new Runnable(){
@Override
public void run() {
while (true){
System.out.println("乌龟领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
}
}
};
Thread thread1 = new Thread(runnable);
thread1.setName("乌龟1线程");
thread1.setPriority(Thread.MAX_PRIORITY);
thread1.start();
Runnable runnable1 = new TortoiseRunnable();
Thread thread2 = new Thread(runnable,"乌龟2线程");
thread2.start();
//一个兔子在跑
Thread.currentThread().setName("主线程");
while (true){
System.out.println("兔子领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
}
}
}
方法三:继承Callable接口
优点:有返回值,可以抛出异常
package com.bjsxt.create3;
import java.util.Random;
import java.util.concurrent.*;
/**
* @author dell CTRL+z 返回上一步
* @data 2021/3/2 有返回值,可以抛出异常
*/
public class RandomCallable implements Callable <Integer> {
@Override
public Integer call() throws Exception {
Thread.sleep(6000);
return new Random().nextInt(10);
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建一个线程对象
Callable<Integer> callable = new RandomCallable();
FutureTask<Integer> task = new FutureTask(callable);
Thread thread = new Thread(task);
//启动线程
thread.start();
//获取返回值
System.out.println(task.isDone());
int result= task.get(); //得不到返回值就一直等待
/*
int result= 0;
try {
result = task.get(3, TimeUnit.SECONDS);
} catch (TimeoutException e) {
e.printStackTrace();
}
*/
System.out.println(task.isDone());
System.out.println(result);
}
}