package ThreadTest.Day2;
/**
* @author CCQ
* 线程停止
* 1.建议线程正常停止->利用次数,不建议死循环
* 2.建议设置使用标志位来停止线程
* 3.不建议使用stop或者destory等过时或者JDK不建议使用的方法
*/
public class Test01 {
public static void main(String[] args) {
thread t =new thread();
new Thread(t,"棋棋").start();
for (int i = 0; i < 1000; i++) {
if (i==900)
t.stop();
System.out.println("main"+i);
}
}
}
class thread implements Runnable{
//设置标志位
private boolean flag =true;
private int i =0;
@Override
public void run() {
while (flag){
System.out.println(Thread.currentThread().getName()+"开启了!"+i++);
}
}
public void stop(){
this.flag =false;
System.out.println("xc结束了!");
}
}
package ThreadTest.Day2;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author CCQ
* 线程休眠sleep()
* 模拟倒计时
*
*/
public class Test02 {
public static void main(String[] args) {
try {
tendowm();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void tendowm() throws InterruptedException {
//获取系统当前时间
Date start =new Date(System.currentTimeMillis());
int num =10;
while (true){
Thread.sleep(1000);
//格式化打印系统当前时间
System.out.println(new SimpleDateFormat("HH:mm:ss").format(start));
//更新系统时间
start =new Date(System.currentTimeMillis());
//输出倒计时
System.out.println(Thread.currentThread().getName()+num--);
if (num<0)
break;
}
}
}
package ThreadTest.Day2;
/**
* 线程的礼让方法
* yield():让当前执行的线程暂停,但不阻塞,将线程的从运行状态转为就绪状态
* 让CPU重新调度,礼让不一定成功,要看CPU调度。
* 类似于我在公交车上坐着(使用CPU),车上来了个老人(别的线程),我从原来的座位站起来(礼让),然后跟老人一起抢座。(CPU调度,所以礼让不一定成功)
*
* 下面的例子并不好,只是看看yield怎么用的。
*/
public class Test03 {
public static void main(String[] args) {
yieldt y =new yieldt();
new Thread(y,"棋棋").start();
new Thread(y,"瑶瑶").start();
}
}
class yieldt implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行!");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"结束!");
}
}
package ThreadTest.Day2;
/**
* @author CCQ
* join()方法,等待此进程执行完毕。
* 调用此方法必须先保证线程已经开启,即已经调用了start方法,同时也应该保证线程未进入终止状态。否则无法正常启用。
* 以下mian线程的循环循环到200时启动VIP线程,并且等待VIP线程执行完(join方法)再继续执行mian线程。
*/
public class Test04 {
public static void main(String[] args) throws InterruptedException {
//main线程启动
//创建VIP线程
joint j =new joint();
Thread t =new Thread(j,"棋棋");
for (int i = 0; i < 1000; i++) {
//当循环到200时,启动VIP线程,并且mian线程需要等待VIP线程执行完毕,(如果这里没有join方法main线程就不会等待,会跟VIP线程一起抢占CPU)
if (i==200){
t.start();
t.join();
}
System.out.println("main线程"+i);
}
}
//一个VIP线程
static class joint implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("我是VIP线程!"+i);
}
}
}
}
package ThreadTest.Day2;
/**
* @author CCQ
* 观察线程的状态
* 线程有五个状态,都是用枚举常量来标识
* thread.getState()方法返回线程的当前状态
* 线程一旦进入死亡状态就不能再次开启
*
*/
public class Test05 {
public static void main(String[] args) throws InterruptedException {
Thread thread =new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(100);
} 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){
Thread.sleep(100);
state =thread.getState();
System.out.println(state);
}
}
}
运行结果:
NEW
RUNNABLE
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
RUNNABLE
睡五秒再执行!
TERMINATED
package ThreadTest.Day2;
/**
* 线程的优先级
* 线程的优先级分为10个:1~10,最高是10,最低是1。如果不设置,则默认为5
* 需要先设置优先级,再启动线程。
*Thread.currentThread().getPriority()获取当前线程的优先级
* 优先级越高的越容易被CPU调度,但这不是一定的,还得看CPU脸色。
*/
public class Test06 {
public static void main(String[] args) {
//main线程优先级
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
Mypriority m =new Mypriority();
Thread t1 =new Thread(m,"棋棋");
Thread t2 =new Thread(m,"瑶瑶");
Thread t3 =new Thread(m,"QQ");
Thread t4 =new Thread(m,"婠婠");
Thread t5 =new Thread(m,"KK");
Thread t6 =new Thread(m,"杨杨");
Thread t7 =new Thread(m,"希希");
//设置不同优先级,启动。
t1.start();
t2.setPriority(Thread.MAX_PRIORITY);
t2.start();
t3.setPriority(Thread.MIN_PRIORITY);
t3.start();
t4.setPriority(7);
t4.start();
t5.setPriority(2);
t5.start();
t6.setPriority(8);
t6.start();
}
}
class Mypriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
输出结果:
main-->5
瑶瑶-->10
婠婠-->7
杨杨-->8
棋棋-->5
KK-->2
QQ-->1
package ThreadTest.Day2;
/**
* @author CCQ
* 线程分为守护线程和用户线程。
* 测试守护线程
* 上帝与人
* setDaemon(boolean n);设置线程为守护线程或者用户线程,参数默认值为false,即用户线程。true为守护线程
* 虚拟机不会保证守护线程是否执行完毕,只会保证用户线程执行。
*
*
*/
public class Test07 {
public static void main(String[] args) {
God god =new God();
You you =new You();
Thread thread =new Thread(god);
//设置上帝线程为守护线程并开启
thread.setDaemon(true);
thread.start();
//你,用户线程
Thread thread1 =new Thread(you);
thread1.start();
}
}
class God implements Runnable{
@Override
public void run() {
while (true)
System.out.println("上帝保护着你!");
}
}
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 30000; i++) {
System.out.println("活着,每一天。");
}
System.out.println("GoodBye World!");
}
}