71 java多线程_3 _线程状态与常见方法 线程状态(基本四状态) 线程常见方法 sleep()休眠 public static void sleep(long millis) 当前线程主动休眠millis毫秒。 package com . wlw . thread ; public class Sleep
71 java多线程_3 _线程状态与常见方法
线程状态(基本四状态)
线程常见方法
sleep()休眠
public static void sleep(long millis) 当前线程主动休眠millis毫秒。
package com.wlw.thread;public class SleepThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"..........."+i);
//主动休眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}package com.wlw.thread;
public class TestSleep {
public static void main(String[] args) {
SleepThread sleepThread1 = new SleepThread();
sleepThread1.start();
SleepThread sleepThread2 = new SleepThread();
sleepThread2.start();
}
}
/*运行结果表现为 停顿一下 运行 停顿一下 运行...... */
yield() 放弃(让出)
public static void yield() 当前线程主动放弃时间片,回到就绪状态,竞争下一次时间片。
package com.wlw.thread;public class YieldThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"-----------"+i);
//主动放弃当前时间片,参与下一次的竞争
Thread.yield();
}
}
}package com.wlw.thread;
public class TestYield {
public static void main(String[] args) {
YieldThread yieldThread1 = new YieldThread();
YieldThread yieldThread2 = new YieldThread();
yieldThread1.start();
yieldThread2.start();
}
}
/*运行结果表现为 两个线程交替运行,偶尔见到一个线程连续运行两次。。。。。。*/
join() 加入
public final void join() 允许其他线程加入到当前线程中。
package com.wlw.thread;public class JoinThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"----------"+i);
//休眠0.5秒
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}package com.wlw.thread;
public class TestJoin {
public static void main(String[] args) {
JoinThread joinThread = new JoinThread();
joinThread.start();
// joinThread 这个线程加入当前线程(当前线程为main),
// 并阻塞当前线程,直到加入的线程(joinThread)执行完毕,再继续执行当前线程
try {
joinThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//主线程执行的
for (int i = 0; i < 15; i++) {
System.out.println(Thread.currentThread().getName()+"==========="+i);
}
}
}
线程对象.setPriority() 设置优先级
public class PriorityThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 50; i++){
System.out.println(Thread.currentThread().getName()+"------------------"+i);
}
}
}package com.wlw.thread;
public class TestPriority {
public static void main(String[] args) {
PriorityThread thread1 = new PriorityThread();
PriorityThread thread2 = new PriorityThread();
PriorityThread thread3 = new PriorityThread();
//设置优先级
thread1.setPriority(1);
thread3.setPriority(10);
//启动线程
thread1.start();
thread2.start();
thread3.start();
}
}
线程对象.setDaemon (true) 设置为守护线程
public class DaemonThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName()+"--------"+i);
//休眠0.5秒
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}package com.wlw.thread;
public class TestDaemon {
public static void main(String[] args) {
//创建线程对象
DaemonThread daemonThread = new DaemonThread();
//将daemonThread 该线程设置为守护线程(后台线程)
//在启动之前设置,并且当前台线程运行完之后,守护线程也会结束
daemonThread.setDaemon(true);
//启动线程
daemonThread.start();
//main 主线程属于前台线程(用户线程)
for (int i = 0; i < 10; i++) {
System.out.println("主线程main=================="+i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
执行结果: 因为前台线程(main)执行完毕,所以守护线程也结束
主线程main==================0
Thread-0--------0
主线程main==================1
主线程main==================2
Thread-0--------1
主线程main==================3
主线程main==================4
Thread-0--------2
主线程main==================5
主线程main==================6
主线程main==================7
Thread-0--------3
主线程main==================8
主线程main==================9
Thread-0--------4
*/
线程等待状态
线程阻塞状态