

| 方法 | 说明 |
|---|---|
| setPriority(int newPriority) | 更改线程的优先级 |
| static void sleep(long millis | 在指定的毫秒数内让当前正在执行的线程休眠 |
| void join() | 等待该线程终止 |
| staticvoidyield() | 暂停当前正在执行的线程对象,并执行其他线程 |
| void interrupt() | 中断线程,别用这个方式 |
| boolean isAlive() | 测试线程是否处于活动状态 |
/**
* @author 缘友一世
* date 2022/11/11-17:20
*/
public class TestStop implements Runnable {
private boolean flag = true;//1 标志位
@Override
public void run() {
int i = 1;
while (flag) {
System.out.println("run...Thread" + i++);
}
}
//设置一个公开的方法改变标志位,停止线程
public void stop() {
this.flag=!flag;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for(int i=1;i<=100;i++) {
System.out.println("main"+i);
if(i==5) {//将停止的循环条件尽量设置的靠前一些,因为主线程执行的非常快,
// 有可能自定义线程,还没来得及停止,主线程就已经执行结束了
//调用停止线程的方法,改变标志位
testStop.stop();
System.out.println("线程停止了");
}
}
}
}

/**
* @author 缘友一世
* date 2022/11/11-17:38
*/
//模拟网络延时
public class TestSleep implements Runnable{
private int ticketNum=10;
@Override
public void run() {
while(true) {
if(ticketNum==0) {
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNum--+"张票");
}
}
public static void main(String[] args) {
TestSleep testSleep = new TestSleep();
new Thread(new TestSleep(),"小王").start();
new Thread(new TestSleep(),"小李").start();
new Thread(new TestSleep(),"小张").start();
}
}

/**
* @author 缘友一世
* date 2022/11/11-17:49
*/
//输出时间
public class TestCountdown {
public static void main(String[] args) throws InterruptedException {
Date date = new Date(System.currentTimeMillis());
while (true) {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
date=new Date(System.currentTimeMillis());
}
}
}

/**
* @author 缘友一世
* date 2022/11/11-17:49
*/
//模拟倒计时
public class TestCountdown {
public static void main(String[] args) throws InterruptedException {
TimeDown();
}
public static void TimeDown() throws InterruptedException {
int num=10;
while(true) {
if(num==0) break;
Thread.sleep(1000);
System.out.println(num--);
}
}
}

/**
* @author 缘友一世
* date 2022/11/11-19:38
*/
//礼让线程,礼让cpu不一定接受
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a1").start();
new Thread(myYield,"a2").start();
}
}
class MyYield implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}


/**
* @author 缘友一世
* date 2022/11/11-19:46
*/
public class TestJoin implements Runnable{
public static void main(String[] args) throws InterruptedException {
//启动线程
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//设置主线程
for(int i=1;i<=8;i++) {
if(i==3) {
thread.join();
}
System.out.println("普通主线程"+i);
}
}
@Override
public void run() {
for(int i=1;i<=3;i++) {
System.out.println("vip用户线程来了"+i);
}
}
}

| 状态 | 说明 |
|---|---|
| NEW | 尚未启动的线程处于此状态。 |
| RUNNABLE | 在Java虚拟机中执行的线程处于此状态 |
| BLOCKED | 被阻塞等待监视器锁定的线程处于此状态 |
| WAITING | 正在等待另一个线程执行特定动作的线程处于此状态。 |
| TIME_DWAITING | 正在等待另一个线程执行动作达到指定等待时间的线程处于此状态 |
| TERMINATED | 已退出的线程处于此状态。 |
/**
* @author 缘友一世
* date 2022/11/11-19:59
*/
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for(int i=1;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("===");
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state);//new
//观察启动线程启动后的状态
thread.start();//启动线程
state=thread.getState();//要及时更新状态
System.out.println(state);//runnable
//TERMINATED--终止
//之线程不终止,就一直输出状态
while (state!=Thread.State.TERMINATED) {
Thread.sleep(10);
state=thread.getState();
System.out.println(state);//TIMED_WAITING
}
}
}

/**
* @author 缘友一世
* date 2022/11/11-20:19
*/
public class TestPriority{
public static void main(String[] args) {//main线程优先级默认为5
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread thread1 = new Thread(myPriority);
Thread thread2 = new Thread(myPriority);
Thread thread3 = new Thread(myPriority);
Thread thread4 = new Thread(myPriority);
Thread thread5 = new Thread(myPriority);
Thread thread6 = new Thread(myPriority);
Thread thread7 = new Thread(myPriority);
Thread thread8 = new Thread(myPriority);
//设置一些优先级 1~10
thread1.start();
thread2.setPriority(4);
thread2.start();
thread3.setPriority(7);
thread3.start();
thread7.setPriority(3);
thread7.start();
thread8.setPriority(Thread.MAX_PRIORITY);
thread8.start();
thread5.setPriority(6);
thread5.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}

- 即使设置了线程的优先级,一样无法确保这个线程一定先执行,因为它有很大的随机性。它并无法控制执行哪个线程,因为线程的执行,是抢占资源后才能执行的操作,而抢点资源时,最多是给于线程优先级较高的线程一点机会而已,能不能抓住可是不一定的。
- 线程优化级较高的线程不一定先执行。线程的执行顺序真正取决于CPU调度器(在Java中是JVM来调度),程序员无法控制。
/**
* @author 缘友一世
* date 2022/11/11-20:50
*/
public class TestDaemon {
public static void main(String[] args) {
Teacher teacher = new Teacher();
Student student = new Student();
//用户线程启动
new Thread(student).start();
Thread thread = new Thread(teacher);
//默认是false,表示用户线程;true 守护线程
thread.setDaemon(true);
//守护线程启动
thread.start();
}
}
class Teacher implements Runnable {
@Override
public void run() {
while(true) {
System.out.println("老师在认真讲课");
}
}
}
class Student implements Runnable {
@Override
public void run() {
System.out.println("上课了");
for(int i=1;i<=5;i++) {
System.out.println("上课中,已过"+i+"分钟");
}
System.out.println("下课了");
}
}
