方法 | 说明 |
---|---|
setPriority(int newPriority) | 更改线程的优先级 |
static void sleep(long millis) | 在指定的毫秒数内让当前正在执行的线程休眠 |
void join() | 等待该线程终止 |
static void yield() | 暂停当前正在执行的线程对象,并执行其他线程 |
void interrupt() | 中断线程,别用这个方式 |
boolean isAlive() | 测试线程是否处于活动状态 |
package com.cnblogs.thread;
/*
测试stop
*/
public class TestStop implements Runnable{
//设置标志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while(flag){
System.out.println("run...." + 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 < 1000; i++) {
System.out.println("main" + i);
if(i == 520){
testStop.stop();
System.out.println("该线程停止了");
}
}
}
}
package com.cnblogs.thread;
import java.util.Date;
/*
模拟倒计时
*/
public class TestSleep {
public static void tenDown() throws InterruptedException {
int num = 10;
while(true){
Thread.sleep(1000);
System.out.println(num--);
if(num <= 0){
break;
}
}
}
public static void main(String[] args) {
Date startTime = new Date(System.currentTimeMillis());//获取系统时间
try {
tenDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.cnblogs.thread;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
打印时间
*/
public class TestSleep {
public static void tenDown() throws InterruptedException {
int num = 10;
while(true){
Thread.sleep(1000);
System.out.println(num--);
if(num <= 0){
break;
}
}
}
public static void main(String[] args) {
Date startTime = new Date(System.currentTimeMillis());//获取系统时间
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());//更新当前时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.cnblogs.thread;
/*
测试礼让线程
*/
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName() + "线程结束执行");
}
}
/*
礼让前效果:
a线程开始执行
a线程结束执行
b线程开始执行
b线程结束执行
礼让后结果:
a线程开始执行
b线程开始执行
a线程结束执行
b线程结束执行
*/
package com.cnblogs.thread;
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 50; 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++) {
if(i >= 200){
thread.join();
}
System.out.println("main线程!!!" + i);
}
}
}
Thread.State
线程可以处于以下状态之一:
New
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
一个线程可以在给定时间点处于一个状态。这些状态是不反映任何操作系统线程状态的虚拟机状态。
package com.cnblogs.thread;
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("0.0");
});
//观测状态
Thread.State state = thread.getState();
System.out.println(state);//NEW
//观测启动后
thread.start();
state = thread.getState();
System.out.println(state);//RUNNABLE
//监听
while (state != Thread.State.TERMINATED){
Thread.sleep(100);
state = thread.getState();
System.out.println(state);//这段时间 TIMED_WAITING
}
//线程结束后 TERMINATED
}
}
Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
线程的优先级用数字表示,范围从1~10
使用以下方式改变或获取优先级
package com.cnblogs.thread;
public class TestPriority{
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);
//默认为5
t1.start();
t2.setPriority(7);
t2.start();
t3.setPriority(Thread.MAX_PRIORITY);
t3.start();
t4.setPriority(Thread.MIN_PRIORITY);
t4.start();
/*
main-->5
Thread-2-->10
Thread-1-->7
Thread-0-->5
Thread-3-->1
*/
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
}
}
package com.cnblogs.thread;
/*
测试守护线程
*/
public class TestDeamon {
public static void main(String[] args) {
God god = new God();
You1 you1 = new You1();
Thread thread = new Thread(god);
thread.setDaemon(true);
thread.start();//上帝线程启动
new Thread(you1).start();
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while(true){
System.out.println("上帝守护着你...");
}
}
}
//你
class You1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("你每天都开心的活着!!!");
}
System.out.println("再见了,世界");
}
}
原文:https://www.cnblogs.com/fangweicheng666/p/15048415.html