Java线程优先级
package com.wkcto.chapter07.method;
/**
* 线程优先级
* 1) 优先级的取值范围: 1 ~ 10
* 2) 所有线程默认的优先级; 5
* 3) 优先级越高, 获得CPU执行权的机率越大
* 4) t1.setPriority( 10 ) 设置线程优先级
* @author 蛙课网
*
*/
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);
}
}
}
Java线程睡眠
package com.wkcto.chapter07.method;
/**
* 线程睡眠 ( 休眠 )
* Thread.sleep( 2000 );
* 1) 是静态方法, 通过Thread类名直接调用
* 2) 睡眠的单位 是毫秒, 1秒 == 1000 毫秒
* 3) sleep()有受检异常需要预处理
* 4) sleep()方法所在的线程睡眠
* @author 蛙课网
*
*/
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线程
for( int i = 1 ; i<=100; i++){
System.out.println( Thread.currentThread().getName() + "-----> " + i);
//当i==10时, 让t1线程睡眠
/*try {
t1.sleep(3000); //虽然是t1调用,实际上是main线程睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
System.out.println( t1.getState() ); //TIMED_WAITING , t1处于sleep()睡眠 状态
}
}
Java线程中断
package com.wkcto.chapter07.method;
/**
* 线程中断
* t1.interrupt(); 中断t1线程
* 一般是把处于睡眠 / 等待中的线程给唤醒
*
* @author 蛙课网
*
*/
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() );
}
}
package com.wkcto.chapter07.method;
/**
* 判断线程的中断状态
* t1.isInterrupted(), 实例方法判断线程的中断状态, 返回true后,不会清除线程的中断标志
* Thread.interrupted(), 静态方法判断线程的中断状态, 如果返回true表示线程被中断了, 然后会清除线程的中断标志
* 再判断线程的中断状态时, 就是false
* @author 蛙课网
*
*/
public class Test07 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// 如果线程没有被中断,就一直打印字符串
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() {
// 如果线程没有被中断,就一直打印字符串
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();
}
}
package com.wkcto.chapter07.method;
/**
* 线程让步
* Thread.yield();
* 把运行中的线程转换为就绪状态
* @author 蛙课网
*
*/
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();
}
}
package com.wkcto.chapter07.method;
/**
* 线程合并
* t1.join(); 在当前线程中加入t1线程,当前线程转为等待状态, 等到t1线程执行完毕后,当前线程再转为就绪状态
* @author 蛙课网
*
*/
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(); //现在加入t1线程, 当前线程转为等待状态, 等到t1执行完后,t2当前线程再转为就绪状态
t1.join(1000); //如果当前线程等待1000毫秒后, 不管t1线程是否结束 ,都会转为就绪状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} , "t2");
t2.start();
}
}
package com.wkcto.chapter07.method;
/**
* 终止线程
* 相办法让run()结束
* 可以为线程设计一个布尔标志, 在run()方法中定期判断这个标志,来决定是否结束 run()
* @author 蛙课网
*
*/
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);
}
}
}