public class Test2 {
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());
}
}
}
运行结果
二、使用标志位使得线程停止运行
创建一个StopThread类实现Runnable接口
//建议线程正常停止,不建议死循环
//建议使用标志位
//不要使用stop和destroy等过时的方法
public class StopThread implements Runnable {
//1.设置一个标志位
private boolean flag=true;
public void run() {
int i=0;
while (flag){
System.out.println("Thread-----"+i++);
}
}
//2.设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag=false;
}
}
测试
public class StopTest {
public static void main(String[] args) {
StopThread stopThread = new StopThread();
new Thread(stopThread).start();
for (int i = 0; i < 1000; i++) {
System.out.println("主线程"+i);
if (i==888){
//调用stop方法切换标志位,让线停止
stopThread.stop();
System.out.println("该线程已经停止!!!");
}
}
}
}
运行结果
三、线程的优先级
创建一个MyPriority类实现Runnable接口
//测试线程的优先级
class MyPriority implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName()+"==========>"+Thread.currentThread().getPriority());
}
}
测试
public class PriorityTest {
public static void main(String[] args) {
//主线程默认的优先级
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.setPriority(1);
t1.start();
t2.setPriority(3);
t2.start();
t3.setPriority(5);
t3.start();
t4.setPriority(7);
t4.start();
t5.setPriority(10);
t5.start();
}
}
运行结果
四、线程礼让
创建一个MyYield类实现Runnable接口
//礼让不一定成功
class MyYield implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
//线程礼让
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
测试
public class YieldTest {
public static void main(String[] args) {
MyYield myYield = new MyYield();
Thread t1 = new Thread(myYield,"a");
Thread t2 = new Thread(myYield,"b");
t1.start();
t2.start();
}
}
运行结果
五、线程插队
创建一个JoinThread类实现Runnable接口
public class JoinThread implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程vip来了"+i);
}
}
}
测试
public class JoinTest {
public static void main(String[] args) throws InterruptedException {
JoinThread joinThread = new JoinThread();
Thread thread = new Thread(joinThread);
thread.start();
//主线程
for (int i = 0; i < 100; i++) {
if (i==20){
//插队
thread.join();
}
System.out.println("主线程"+i);
}
}
}
运行结果
六、守护线程
创建一个MyGog类实现Runnable接口
class MyGog implements Runnable{
public void run() {
while (true){
System.out.println("保佑你");
}
}
}
创建一个MyDaemon类实现Runnable接口
class MyDaemon implements Runnable{
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("开心每一天");
}
System.out.println("再见");
}
}
测试
public class DaemonTest {
public static void main(String[] args) {
MyGog myGog = new MyGog();
MyDaemon myDaemon = new MyDaemon();
Thread thread = new Thread(myGog);
//默认为falae,为用户线程,正常的都为用户线程
thread.setDaemon(true);
//启动守护线程
thread.start();
//用户线程启动
new Thread(myDaemon).start();
}
}