多线程的状态:
1.线程停止
- 建议线程正常停止—>利用次数,不建议死循环
- 建议使用标志位—>设置一个标志位
- 不要使用stop或者destroy等过时,或者JDK不建议使用的方法
自定义stop,可以看到threa停止,但是mian函数还在跑
package statue;
public class TestStop implements Runnable{
boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("run Thread"+ i++);
}
}
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){
testStop.stop();
System.out.println("run停止了!");
}
}
}
}
2.线程休眠
- sleep可以模拟网络延时,放大问题的发生性。
- 应用例子:多线程卖票,倒计时,获取系统时间。
- 每一个对象都有一个锁,sleep不会释放锁。
package statue;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep {
public static void main(String[] args) throws InterruptedException {
try {
tenDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
Date startTime= new Date(System.currentTimeMillis());
while (true)
{
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:MM:SS").format(startTime));
startTime=new Date(System.currentTimeMillis());
}
}
public static void tenDown() throws InterruptedException {
int num=3;
while (true)
{
Thread.sleep(1000);
System.out.println(num--);
if (num<=0){
break;
}
}
}
}
结果
3
2
1
16:02:508
16:02:529
3.线程礼让
- 礼让线程,让当前正在执行的线程暂停,但是不阻塞
- 将程序从运行状态变为就绪状态
- cpu重新调度,但是礼让不一定成功,看cpu的心情
- 礼让,同时竞争。例如当前A正在运行,B就绪。礼让后,A退出运行状态,此时A和B同时竞争资源。若A运行,则礼让不成功。
package statue;
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{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程停止");
}
}
4.Join
- Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞
- 可以想象插队
package statue;
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin=new TestJoin();
Thread thread =new Thread(testJoin);
thread.start();
for (int i = 0; i < 60; i++) {
if (i==20){
thread.join();
}
System.out.println("main"+i);
}
}
}
5.观测线程状态
使用:Thread.State state = thread.getState();
public class TestState {
public static void main(String[] args) {
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);
thread.start();
state = thread.getState();
System.out.println(state);
while (state != Thread.State.TERMINATED) {
try {
Thread.sleep(100);
state = thread.getState();
System.out.println(state);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
6.线程的优先级
getPriority() setPriority(int xxx)
- 先设置优先级 再启动
- main 默认优先级5
- 优先级 1-10
package statue;
public class TestPriority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority );
Thread t2 = new Thread(myPriority );
Thread t3 = new Thread(myPriority );
Thread t4 = new Thread(myPriority );
Thread t5 = new Thread(myPriority );
Thread t6 = new Thread(myPriority );
t1.start();
t2.setPriority(3);
t2.start();
t3.setPriority(5);
t3.start();
t4.setPriority(7);
t4.start();
t5.setPriority(Thread.MIN_PRIORITY);
t5.start();
t6.setPriority(Thread.MAX_PRIORITY);
t6.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
}
}
可以看出不是优先级高的就会先执行,线程优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用了,看CPU调度
main-->5
Thread-0--->5
Thread-4--->1
Thread-5--->10
Thread-3--->7
Thread-1--->3
Thread-2--->5
7.守护线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 如,后台记录操作日志,监控内存,垃圾回收等待。。。
- 守护线程在用户线程执行完毕之后也会执行完毕,不过JVM需要一点时间才停止
- thread.setDaemon(true);//默认为flase 为用户线程, true为守护线程
上帝守护着你
package statue;
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you=new You();
Thread thread = new Thread(god);
thread.setDaemon(true);
thread.start();
new Thread(you).start();
}
}
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝守护着你");
}
}
}
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i <36500 ; i++) {
System.out.println("开心着活着");
}
System.out.println("----goodbye!------");
}
}
|