package com.wkcto.chapter07.method;
/**
- Thread priority
-
- Priority value range: 1 ~ 10
-
- The default priority of all threads; 5
-
- The higher the priority, the greater the chance of obtaining CPU execution rights
-
- t1.setPriority( 10) Set thread priority
- @author frog class network
*/
public class Test04 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 1; i <= 100; i++){
System.out.println( Thread.currentThread().getName() + "-->" + i);
}
}
} , "t1");
t1.setPriority(1); //设置优先级
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 1; i <= 100; i++){
System.out.println( Thread.currentThread().getName() + "-->" + i);
}
}
} , "t2");
t2.setPriority(10); //设置优先级
t2.start();
//打印线程的优先级
System.out.println( "t1 priority: " + t1.getPriority());
System.out.println( "t2 priority: " + t2.getPriority());
System.out.println( "main priority: " + Thread.currentThread().getPriority());
//main线程
for(int i = 1; i <= 100; i++){
System.out.println( Thread.currentThread().getName() + "-->" + i);
}
}
}
Click and drag to move
Java thread sleep
package com.wkcto.chapter07.method;
/**
- Thread sleep (sleep)
- Thread.sleep( 2000 );
-
1) 是静态方法, 通过Thread类名直接调用
-
2) 睡眠的单位 是毫秒, 1秒 == 1000 毫秒
-
3) sleep()有受检异常需要预处理
-
4) sleep()方法所在的线程睡眠
- @author frog class network
*/
public class Test05 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for( int i = 1 ; i<=100; i++){
System.out.println( Thread.currentThread().getName() + "--> " + i);
//当 i == 50 时, 线程休眠
if ( i == 50 ) {
//run()是重写了Runnable接口中run(),不能声明抛出异常,只能捕获处理
try {
Thread.sleep(3000); //睡眠3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}, "t1");
t1.start();
// t1 = null;
//main thread
for( int i = 1; i<=100; i++){ System.out.println( Thread.currentThread().getName() + "-----> "+ i); //When i==10, let t1 thread sleep / try { t1.sleep(3000); //Although it is t1 call, it is actually the main thread sleep } catch (InterruptedException e) { e.printStackTrace( ); } / }
System.out.println( t1.getState() ); //TIMED_WAITING , t1处于sleep()睡眠 状态
}
}
Click and drag to move
Java thread interruption
package com.wkcto.chapter07.method;
/**
- Thread interruption
-
t1.interrupt(); 中断t1线程
-
一般是把处于睡眠 / 等待中的线程给唤醒
- @author frog class network
*/
public class Test06 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for( int i = 1 ; i<=100; i++){
System.out.println( Thread.currentThread().getName() + "--> " + i);
//当 i == 50 时, 线程休眠
if ( i == 50 ) {
try {
Thread.sleep(10000); //睡眠10秒
} catch (InterruptedException e) {
// e.printStackTrace ();
}
}
}
}
}, “T1”);
t1.start ();
//main线程
for( int i = 1 ; i<=100; i++){
System.out.println( Thread.currentThread().getName() + "--> " + i);
}
//当main线程结束 , 把t1线程唤醒
t1.interrupt(); //中断t1线程的睡眠, 会抛出中断异常
// System.out.println( t1.isInterrupted() );
}
}
Click and drag to move
package com.wkcto.chapter07.method;
/**
- Determine the interrupt status of the thread
-
t1.isInterrupted(), 实例方法判断线程的中断状态, 返回true后,不会清除线程的中断标志
-
Thread.interrupted(), 静态方法判断线程的中断状态, 如果返回true表示线程被中断了, 然后会清除线程的中断标志
-
再判断线程的中断状态时, 就是false
- @author frog class network
*/
public class Test07 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// If the thread is not interrupted, it keeps printing the string
while(! Thread.currentThread().isInterrupted() ){ System.out.println("wkcto"); } System.out.println("11: "+ Thread.currentThread().isInterrupted()); } }); t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// If the thread is not interrupted, always print the string
while(! Thread.interrupted() ){ System.out.println(“bjpowernode”); } System.out.println("22: "+ Thread.currentThread( ).isInterrupted()); } }); t2.start();
// main线程
for (int i = 1; i <= 50; i++) {
System.out.println(Thread.currentThread().getName() + "--> " + i);
}
//main线程结束, 就中断t1线程
t1.interrupt();
t2.interrupt();
}
}
Click and drag to move
Java thread concessions
package com.wkcto.chapter07.method;
/**
- Thread concession
-
Thread.yield();
-
把运行中的线程转换为就绪状态
- @author frog class network
*/
public class Test08 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "--> " + i);
//当 i的值是10的倍数时, 线程让步
if ( i % 10 == 0) {
Thread.yield(); //转换为就绪状态
}
}
}
} , "t1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "-====> " + i);
}
}
} , "t2");
t2.start();
}
}
Click and drag to move
Java thread merging
package com.wkcto.chapter07.method;
/**
- Thread merging
-
t1.join(); 在当前线程中加入t1线程,当前线程转为等待状态, 等到t1线程执行完毕后,当前线程再转为就绪状态
- @author frog class network
*/
public class Test09 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "--> " + i);
}
}
} , "t1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "-====> " + i);
//当i==10时, 把t1线程合并进来
if ( i == 10 ) {
try {
// t1.join(); //Now join the t1 thread, the current thread turns to the waiting state, and after the execution of t1, the current thread of t2 turns to the ready state
t1.join(1000); //If the current thread waits for 1000 after ms, regardless of whether the end of the thread t1, into the ready state will
} the catch (InterruptedException E) { e.printStackTrace (); } } } } }, "T2"); t2.start (); }
}
Click and drag to move
Java thread termination
package com.wkcto.chapter07.method;
/**
- Terminate thread
- Phase method to end run()
- You can design a boolean flag for the thread, and periodically judge this flag in the run() method to decide whether to end the run()
- @author frog class network
*/
public class Test11 {
public static void main(String[] args) {
SubThread1 thread1 = new SubThread1();
thread1.start();
Prime3 prime3 = new Prime3();
Thread t2 = new Thread(prime3);
t2.start();
//main线程
for (int i = 1; i <= 50; i++) {
System.out.println(Thread.currentThread().getName() + "========> " + i);
}
//main线程结束 , 终止t1线程
thread1.stopping = true;
prime3.running = false;
}
}
class SubThread1 extends Thread{
boolean stopping = false;
@Override
public void run() {
for (int i = 1; i <= 500; i++) {
if (stopping) {
return; //结束方法的执行
}
System.out.println(Thread.currentThread().getName() + "–> " + i);
}
}
}
class Prime3 implements Runnable{
boolean running = true;
@Override
public void run() {
for( int i = 1; running && i<=500; i++){
System.out.println(Thread.currentThread().getName() + "–> " + i);
}
}
}
点击并拖拽以移动