目录
1 线程停止
Thread的stop方法已过时,通常情况下需要程序员自己写一些方法让线程停止。
通常可以在线程内部做一个flag标识,通过flag判断线程是否继续执行
public class ThreadStop {
public static void main(String[] args){
RunnableThread rt = new RunnableThread();
Thread thr = new Thread(rt);
thr.start();
for(int i = 1; i <= 20; i++){
if(i == 10){
rt.stop();
}
System.out.println("主线程:"+i);
}
}
}
class RunnableThread implements Runnable{
private volatile boolean flag = true;
int i = 0;
@Override
public void run() {
while(flag){
i++;
System.out.println("RunnableThread执行第"+i+"次");
}
}
public void stop(){
this.flag = false;
System.out.println("RunnableThread线程停止");
}
}
2 线程让步
调用Thread.yield();的当前线程会让出CPU资源,然后重新参与资源竞争。竞争结果是有可能继续占用CPU资源,有可能不是
@Test
public void testYield() throws InterruptedException, ExecutionException {
RunnableThread runnableThread = new RunnableThread();
Thread thr = new Thread(runnableThread);
thr.start();
for (int i = 1; i <= 100; i++) {
System.out.println(i);
// 主线程每执行10次,让步一次
if (i % 10 == 0) {
Thread.yield();
}
}
}
class RunnableThread implements Runnable {
boolean flag = true;
int i = 0;
@Override
public void run() {
while (flag) {
System.out.println(Thread.currentThread().getName() + "执行" + (i ++) + "次");
}
}
}
观察输出结果可以发现每次让步后有可能主线程继续运行,有可能切换到子线程运行
3 线程加入
@Test
public void testJoin() throws InterruptedException, ExecutionException {
RunnableThread runnableThread = new RunnableThread();
RunnableThread runnableThread2 = new RunnableThread();
Thread thr = new Thread(runnableThread, "子线程一");
Thread thr2 = new Thread(runnableThread2, "子线程二");
thr.start();
thr2.start();
for (int i = 1; i <= 100; i++) {
System.out.println(i);
// 主线程执行10次后然子线程加入
if (i == 10) {
thr.join();
}
}
}
class RunnableThread implements Runnable {
boolean flag = true;
int i = 0;
@Override
public void run() {
while (flag) {
System.out.println(Thread.currentThread().getName() + "执行" + (i ++) + "次");
if (i == 300) {
break;
}
}
}
}
观察输出结果可以发现主线程输出10后两个子线程交替执行。直到两个子线程执行完毕,主线程才继续运行
4 线程通信(wait和notify)
wait和notify是Object类的方法,某个对象执行wait或notify的时候当前线程一定要先获取到synchronized锁,否则会报IllegalMonitorStateException,有兴趣的同学可以研究一下相关的JIN方法
实现功能:创建两个线程,交替输出各自线程的名字
/**
* 当前标志位
*/
private volatile int flag = 0;
/**
* 定义一个锁
*/
private final String lock = "";
@Test
public void testWaitAndNotify() throws InterruptedException, ExecutionException {
// 线程一执行标志位是0,下个线程(线程二)执行标志位是1
RunnableThread runnableThread = new RunnableThread(0, 1);
// 线程二执行标志位是1,下个线程(线程一)执行标志位是0
RunnableThread runnableThread2 = new RunnableThread(1, 0);
Thread thr = new Thread(runnableThread, "线程一");
Thread thr2 = new Thread(runnableThread2, "线程二");
thr.start();
thr2.start();
TimeUnit.HOURS.sleep(1);
}
class RunnableThread implements Runnable {
/**
* 当前线程执行标识
*/
private volatile int symbol;
/**
* 下个线程执行标识
*/
private volatile int next;
public RunnableThread(int symbol, int next) {
this.symbol = symbol;
this.next = next;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
try {
Thread.sleep(500);
} catch (InterruptedException ignore) {}
if (flag != symbol) {
try {
// 调用wait和notify的方法一定要是synchronized锁住的对象
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (flag == symbol) {
System.out.println(Thread.currentThread().getName());
flag = next;
// 调用wait和notify的方法一定要是synchronized锁住的对象
lock.notify();
}
}
}
}
}
这样我们就可以在控制台看到线程一,线程二交替输出