package com.cn;
/*
* 1、建议线程正常停止--》利用次数,不建议死循环
* 2、建议使用标志位---》设置一个标志位
* 3、不要使用stop或者destroy等过时方法或者JDK不建议使用的方法
* */
public class TestStop implements Runnable {
//设置一个标志位
private 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 < 904; i++) {
System.out.println("main" + i);
if (i == 900) {
//调用stop方法切换标志位,让线程停止
testStop.Stop();
System.out.println("线程该停止了!");
}
}
}
}
package com.cn;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep {
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());
}
}
}
package com.cn;
//测试礼让线程
//礼让不一定能成功
public class TestYied {
public static void main(String[] args) {
MyYied myYied = new MyYied();
new Thread(myYied, "小明").start();
new Thread(myYied, "小黄").start();
}
}
class MyYied implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程执行开始");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程执行结束");
}
}
package com.cn;
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 200; 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 < 1000; i++) {
while (i == 200) {
thread.join(); //插队
}
System.out.println("main"+i);
}
}
}
package com.cn;
import org.apache.commons.io.output.ThresholdingOutputStream;
public class TestState {
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);//new
//观察启动
thread.start();//启动线程
state = thread.getState();
System.out.println(state); //run
while (state != Thread.State.TERMINATED) {
//只要线程不终止就一直输出状态
Thread.sleep(100);
state = thread.getState();//更新线程状态
System.out.println(state); //输出状态
}
}
}
package com.cn;
public class TestPriority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
Thread thread = new Thread(new MyPriority(), "线程1");
thread.start();
Thread thread1 = new Thread(new MyPriority(), "线程2");
thread1.setPriority(Thread.MAX_PRIORITY);
thread1.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
}
}