目录
1:创建线程
1.1:继承Thread类
public class Demo_01 {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
while(true) {
System.out.println("2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
class MyThread extends Thread {
@Override
public void run() {
while(true) {
System.out.println("1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
1.2实现Runnable接口
/**
* 实现Runnable接口线程
*/
public class ThreadDemo4 {
public static void main(String[] args) {
//创建Runnable
MyThread2 myThread2 = new MyThread2();
//创建一个线程
Thread thread = new Thread(myThread2);
//启动线程
thread.start();
}
}
class MyThread2 implements Runnable{
@Override
public void run() {
Thread thread = Thread.currentThread();//得到当前线程
System.out.println("线程执行:" + thread.getName());
}
}
1.3:匿名内部类
public class ThreadDemo5 {
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();
}
}
1.4:lambda表达式
public class ThreadDemo6 {
public static void main(String[] args) {
Thread thread = new Thread(()->{
Thread t = Thread.currentThread();
System.out.println("任务执行:" + t.getName());
});
thread.start();
}
}
1.5: FutureTask + Callable
public class Demo_1101_Callable {
public static void main(String[] args) {
// 通过Callable定义线程的任务
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
// 这里是具体要处理的任务
System.out.println("执行运算..");
int a = 10 + 20;
// 等待3秒返回
TimeUnit.SECONDS.sleep(3);
throw new Exception("任务执行过程中出现异常");
// return a;
}
};
// Callable要配合FutureTask一起使用,FutureTask是用来获取Callable的执行结果的
FutureTask<Integer> futureTask = new FutureTask<>(callable);
// FutureTask当做构造参数传入Thread的构造方法里
Thread thread = new Thread(futureTask);
// 启动线程
thread.start();
try {
// 阻塞等待Callable任务的执行结果
Integer result = futureTask.get();
System.out.println("线程的执行结果:" + result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
System.out.println("捕获到任务异常:" + e.getMessage());
}
}
}
2.线程属性
public class Demo_05 {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
int count =5;
while(count--!=0) {
System.out.println("子线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"thread-0");
//设置线程为后台线程
thread.setDaemon(true);
thread.start();
System.out.println("线程的ID(JVM):"+thread.getId());
System.out.println("线程的名字:"+thread.getName());
//TimeUnit.SECONDS.sleep(1);
System.out.println("线程的状态:"+thread.getState());
System.out.println("线程的优先级:"+thread.getPriority());
System.out.println("线程是否为后台线程:"+thread.isDaemon());
//thread.interrupt();
System.out.println("线程是否被打断:"+thread.isInterrupted());
System.out.println("线程是否存活:"+thread.isAlive());
thread.join();
System.out.println("线程是否存活:"+thread.isAlive());
}
}
3:线程中断
3.1:使用自定义标识符来终止线程
public class ThreadInterrupet {
private volatile static boolean flag = false;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
while (!flag){
System.out.println("执行任务...");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("执行完成");
});
thread.start();
Thread.sleep(3000);
//终止线程
System.out.println("停止执行");
flag = true;
}
}
3.2:使用 interrupt() 终止线程
interrupt(): 如果被打断线程正在 sleep , wait , join 会导致被打断的线程抛InterruptedException ,并清除打断标记,打断标记为false ;如果打断的正常运行的线程,则会设置打断标记为true
public class Test01 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
while (!Thread.interrupted()){
System.out.println("执行任务...");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
System.out.println("执行完成");
});
thread.start();
Thread.sleep(3000);
//终止线程
System.out.println("停止执行");
thread.interrupt();
}
}
4:线程等待
public class Test01 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(()->{
System.out.println("t1执行任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1执行完成");
});
t1.start();
Thread t2 = new Thread(()->{
System.out.println("t2执行任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2执行完成");
});
t2.start();
t1.join();
t2.join();
System.out.println("t1,t2任务执行完成");
}
}
5:线程休眠
5.1:sleep()休眠
public class Test01 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("睡眠中断");
}
});
thread.start();
Thread.sleep(1000);
System.out.println("终止子线程thread");
thread.interrupt();
}
}
5.2:TimeUtil休眠
import java.util.concurrent.TimeUnit;
public class Test01 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("睡眠中断");
}
});
thread.start();
Thread.sleep(1000);
System.out.println("终止子线程thread");
thread.interrupt();
}
}
6:获取线程实例
public class Test01 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+":睡眠中断");
}
});
thread.start();
Thread.sleep(1000);
// 通过Thread.currentThread()方法获取线程实例对象
System.out.println(Thread.currentThread().getName()+":终止子线程thread");
thread.interrupt();
}
}
7:yield让出执行权
线程调用yield()方法后时继续执行还是等待下一轮调度,取决于线程调度器是否释放当前线程的cpu执行权,而这个过程是随机的,正是因为这个不确定性所以一般很少使用这个方法;在调试或者测试的时候这个方法或许可以帮助复现由于并发竞争条件导致的问题,在设计并发控制时或许可以使用到
public class Test01 {
public static void main(String[] args) {
Thread thread1 = new Thread(()->{
System.out.println("线程1 执行当中");
System.out.println("线程1 暗示线程,可以进行新一轮的线程调度");
Thread.yield();
System.out.println("线程1 放弃cpu执行权");
});
thread1.start();
Thread thread2 = new Thread(()->{
System.out.println("线程2 执行当中");
System.out.println("线程2 暗示线程,可以进行新一轮的线程调度");
Thread.yield();
System.out.println("线程2 放弃cpu执行权");
});
thread2.start();
}
}