| 方法 |
说明 |
| setPriority(int newPriority) |
更改线程的优先级 |
| static void sleep(long millis) |
在指定的毫秒数内让当前正在执行的线程体休眠 |
| void join() |
等待该线程终止 |
| static void yield() |
暂停当前正在执行的线程,并执行其他线程 |
| void interrupt() |
中断线程(一般不建议使用××××) |
| boolean isAlive() |
测试线程是否处于活动状态 |
停止
package com.Thread.lesson02;
/**
* 测试stop
* 1.建议线程正常停止--->利用次数,不建议死循环
* 2.建议使用标志位,
* 3.不要使用stop、destroy等过时的方法
*/
public class TestStop implements Runnable{
private boolean flag = true;
@Override
public void run() {
int i=0;
while(flag) {
System.out.println("线程================="+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 < 100; i++) {
System.out.println("main里面的"+i);
if (i==50){
testStop.stop();
System.out.println("该线程停止了");
}
}
}
}
线程休眠_sleep
package com.Thread.lesson02;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep {
//模拟倒计时
public static void tenDown() throws InterruptedException {
int num = 10;
while (num>=0){
Thread.sleep(1000);
System.out.println(num--);
}
}
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(startTime);
startTime = new Date(System.currentTimeMillis());//更新时间
}
}
}
线程礼让_Yield
package com.Thread.lesson02;
/**
*线程礼让:让当前正在执行的线程暂停,但不阻塞
*但是礼让不一定成功
* @author 长空扯淡
*/
public class TestYield{
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"桐人").start();
new Thread(myYield,"亚丝娜").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止");
}
}
线程强制执行_join
package com.Thread.lesson02;
/**
* 测试Join
* 强制执行-----可以想象成插队
*/
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("大小姐闪开,通通驾到");
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
new Thread(testJoin).start();
for (int i = 0; i < 20; i++) {
if (i==10){
new Thread().join();//插队
}
System.out.println("大小姐");
}
}
}
线程的优先级
package com.Thread.lesson02;
/**
* 线程优先级
* 优先级的范围是1~10
* 注意:不一定优先级高的就一定先执行,还是看cpu的心情
* @author 长空扯淡
*/
import javafx.scene.layout.Priority;
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级 默认为5
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);
//先设置优先级,在启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(Thread.MAX_PRIORITY);
t3.start();
t4.setPriority(5);
t4.start();
t5.setPriority(6);
t5.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}