多线程
线程休眠
- sleep指定当前线程阻塞的毫秒数
- sleep需要抛出InterruptedException
- sleep时间到达后线程进入就绪状态
- sleep可以模拟网络延时,倒计时等
- 每个对象都有一个锁,sleep不会释放锁
模拟网络延时
//模拟网络延时:放大问题的发生概率
public class TestSleep implements Runnable{
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
System.out.println("正在加载" + i);
if (i==99){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TestSleep testSleep = new TestSleep();
new Thread(testSleep).start();
}
}
模拟倒计时
public class TestSleep2 implements Runnable{
@Override
public void run() {
int i = 10;
while (true){
System.out.println(i--);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i==0){
break;
}
}
}
public static void main(String[] args) {
TestSleep2 testSleep2 = new TestSleep2();
new Thread(testSleep2).start();
}
}
打印系统时间
public class TestSleep2 implements Runnable{
@Override
public void run() {
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();
}
}
}
public static void main(String[] args) {
TestSleep2 testSleep2 = new TestSleep2();
new Thread(testSleep2).start();
}
}
线程礼让
- 礼让线程,让当前正在执行的线程暂停,但不阻塞
- 将线程从运行状态转为就绪状态
- 让cpu重新调度,礼让不一定成功
public class TestYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"执行结束");
}
public static void main(String[] args) {
TestYield testYield = new TestYield();
new Thread(testYield,"a").start();
new Thread(testYield,"b").start();
}
}
线程强制执行(一般不建议使用)
![]()
观测线程状态
public class TestState {
public static void main(String[] args) {
Thread thread = new Thread(()->{//Lambda表达式,即Runnable实现类
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);
} catch (InterruptedException e) {
e.printStackTrace();
}
state = thread.getState();//更新线程状态
System.out.println(state);
}
}
}
线程的优先级
- main方法的默认优先级是5,优先级最小为1,最大为10
- 先设置优先级再执行
- 优先级低只意味着获得调度的概率低
public class Test {
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);
t1.start();
t2.setPriority(2);
t2.start();
t3.setPriority(7);
t3.start();
t4.setPriority(1);
t4.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
//输出线程名和线程优先级
System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority());
}
}