Thread.sleep() 线程休眠
作用:模拟网络延迟,放大出错概率 倒计时.....
package hk.Thread.deom5;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
public class Text1{
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());
}
}
}
方法 | 说明 |
setPriority(int newPriority) | 更改线程优先级 |
static void sleep(long millis) | 在指定毫秒内使当前线程休眠 |
void join() | 等待该线程终止 |
static void yield() | 暂停当前执行的线程对象,并执行其他线程 |
void interrupt() | 中断线程(不建议使用) |
boolean isAlive() | 测试线程是否活动 |
static void yield() 礼让其他线程;有可能礼让失败 【多核可能测不出来效果】
package hk.Thread.deom5;
public class Text2 {
public static void main(String[] args) {
Textyield textyield = new Textyield();
new Thread(textyield,"0").start();
new Thread(textyield,"1").start();
}
}
class Textyield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始。。。");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程结束。。。");
}
}
void join() 其他线程阻塞,等待该线程执行完,其他线程才执行
package hk.Thread.deom5;
public class Text3 implements Runnable{
public static void main(String[] args) throws InterruptedException {
Text3 text3 = new Text3();
Thread thread = new Thread(text3);
thread.start();
for (int i = 0; i < 5; i++) {
System.out.println("main()——"+i);
thread.join();
}
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("join()线程——"+i);
}
}
}
大概率就是这结果了
线程状态
NEW | 线程尚未启动 |
RUNNABLE | 在JAVA虚拟机中执行 |
BLOCKED | 被阻塞等待监视器锁定的 |
WAITING | 等待另一个线程执行特定动作 |
TIMED_WAITING | 等待另一个线程达到指定时间的线程 |
TERMINATED | 已退出的 |
thread.getState() 获取线程状态
一般线程的流程
package hk.Thread.deom5;
public class Text4 {
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);
thread.start();
state=thread.getState();
System.out.println(state);
System.out.println("===========================\n===========================");
while (state!=Thread.State.TERMINATED){
Thread.sleep(500);
state=thread.getState();
System.out.println(state);
}
System.out.println("===========================\n===========================");
state=thread.getState();
System.out.println(state);
}
线程优先级 范围 1——10
先设置优先级;再启动线程 优先级高的更容易先执行
Thread.currentThread().getPriority() 获取该线程优先级
thread3.setPriority(5) 设置线程优先级
package hk.Thread.deom5;
public class Text5 {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"------"+Thread.currentThread().getPriority());
TextPriority textPriority = new TextPriority();
Thread thread1 = new Thread(textPriority,"1");
Thread thread2 = new Thread(textPriority,"2");
Thread thread3 = new Thread(textPriority,"3");
Thread thread4 = new Thread(textPriority,"4");
//先设置优先级;再启动线程
thread1.start();
thread2.setPriority(1);
thread2.start();
thread3.setPriority(5);
thread3.start();
thread4.setPriority(10);
thread4.start();
}
}
class TextPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"------"+Thread.currentThread().getPriority());
}
}
一般结果
线程分为 用户线程 与 守护线程
虚拟机必须确保用户线程 执行完毕 不用等待守护线程执行完毕
守护线程:垃圾回收 、后台记录早作人质操作日志、监控内存...
设置线程是否为《守护线程》 thread.setDaemon(false) 默认是False【用户线程】true【守护线程】
package hk.Thread.deom5;
public class Text6 {
public static void main(String[] args) {
UserThread userThread = new UserThread();
DaemonThread daemonThread = new DaemonThread();
Thread thread = new Thread(daemonThread);
thread.setDaemon(true);
Thread thread1 = new Thread(userThread);
thread.start();
thread1.start();
}
}
class UserThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5 ; i++) {
System.out.println("User——"+i);
}
}
}
class DaemonThread implements Runnable{
@Override
public void run() {
while (true){
System.out.println("DaemonThread");
}
}
}