JavaSE自学笔记Real_007(多线程基础)
程序运行状态
创建状态、就绪状态、阻塞状态、运行状态、死亡状态
线程的函数方法
- setPriority(int newPriority):更改线程的优先级
- static void sleep(long millis):在指定的毫秒数内让当前正在执行的线程休眠
- void join():等待该线程终止
- static void yield():暂停当前正在执行的线程对象,并执行其他线程
- void interrupt():中断线程,别用这个方式
- boolean isAlive():测试线程是否处于活动状态
线程的停止(stop)
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
int i = 0;
while(this.flag){
System.out.println("run...Thread" + i++);
}
}
//2、设置一个公开的方法来停止线程,转换标志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main" + i);
if(i == 900){
//调用stop方法,切换标志位,让线程停止
testStop.stop();
System.out.println("线程已停止!!");
}
}
}
}
线程的休眠(sleep)
1、模拟网络延时
//模拟网络延时:方法问题的发生性
public class TestSleep implements Runnable{
//票数
private int ticketNums = 10;
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
while(true){
if(ticketNums <= 0){
break;
}
//模拟延时
try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "-->拿到了第" + ticketNums-- + "票");
}
}
public static void main(String[] args) {
TestSleep ticket = new TestSleep();
new Thread(ticket, "小明").start();
new Thread(ticket, "老师").start();
new Thread(ticket, "黄牛").start();
}
}
结果对比:
1、有网络延时的结果:
黄牛-->拿到了第9票
老师-->拿到了第9票
小明-->拿到了第10票
黄牛-->拿到了第8票
小明-->拿到了第6票
老师-->拿到了第7票
老师-->拿到了第4票
小明-->拿到了第3票
黄牛-->拿到了第5票
小明-->拿到了第2票
黄牛-->拿到了第2票
老师-->拿到了第2票
黄牛-->拿到了第1票
小明-->拿到了第0票
老师-->拿到了第0票
Process finished with exit code 0
//============================================
2、没有网络延时的结果
小明-->拿到了第10票
小明-->拿到了第7票
小明-->拿到了第6票
小明-->拿到了第5票
小明-->拿到了第4票
小明-->拿到了第3票
小明-->拿到了第2票
小明-->拿到了第1票
黄牛-->拿到了第8票
老师-->拿到了第9票
Process finished with exit code 0
2、模拟倒计时
import java.util.Date;
import java.text.SimpleDateFormat;
public class TestSleep3 {
//打印版系统当前时间
public static void main(String[] args) {
Date startTime = new Date(System.currentTimeMillis()); //获取当前时间
while(true){
try{
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm;ss").format(startTime));
startTime = new Date(System.currentTimeMillis()); //更显当前时间
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
线程的礼让(yield)
礼让线程,让当前正在执行的线程暂停,但是不阻塞
将线程从远程状态转为就绪状态
让CPU重新调度,礼让不一定成功,看CPU的心情进行随机调度
//测试礼让线程
//礼让不一定成功
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield, "a").start();
new Thread(myYield, "b").start();
}
}
class MyYield implements Runnable{
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "线程开始执行");
Thread.yield(); //礼让
System.out.println(Thread.currentThread().getName() + "线程停止执行");
}
}
线程的合并(join)
join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞
public class TestJoin implements Runnable{
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
for(int i = 0; i < 1000;i++){
System.out.println("线程VIP来插队" + i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动主线程
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
//主线程
for (int i = 0; i < 500; i++) {
if(i == 200){
thread.start();
thread.join(); //插队
}
System.out.println("main" + i);
}
}
}
观测线程状态
import javax.swing.*;
//观察测试线程的状态
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() ->{
for (int i = 0; i < 5; i++) {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println("///");
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state); //NEW
//观察启动后
thread.start(); //开启线程
state = thread.getState();
System.out.println(state);
while(state != Thread.State.TERMINATED){ //只要线程不终止,就一直输出状态
Thread.sleep(100);
state = thread.getState(); //更新线程状态
System.out.println(state); //输出状态
}
//thread.start(); // 死亡之后的线程就不会再重新启动了,执行到本代码的时候系统会报错
}
}