线程基础-线程的使用(一)
1.获取当前线程
package com. ysf ;
public class Tst01CurrentThread {
public static void main ( String [ ] args) {
Thread thread = Thread . currentThread ( ) ;
System . out. println ( thread) ;
}
}
2.设置线程名称
创建线程或线程池时,必须指定线程或线程池的有意义名称,方便出错时回溯
package com. ysf ;
import java. util. concurrent. atomic. AtomicInteger ;
public class Tst02SetThreadName {
static AtomicInteger count = new AtomicInteger ( ) ;
public static void main ( String [ ] args) {
Thread t = new Thread ( ( ) -> {
System . out. println ( Thread . currentThread ( ) . getName ( ) ) ;
} , "module-func-" + count. get ( ) ) ;
t. start ( ) ;
}
}
3.线程的优先级
线程的优先级是指CPU调度的优先级 线程优先级最大值:10,最小值:1,默认值5 线程优先级并非决定性因素,效果可能并不会很明显
package com. ysf ;
import java. util. concurrent. TimeUnit ;
public class Tst03SetThreadPriority {
public static void main ( String [ ] args) throws InterruptedException {
Thread t1 = new Thread ( ( ) -> {
for ( int i = 0 ; i< 100 ; i++ ) {
String name = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( name + "==>" + i) ;
}
} , "priority-1" ) ;
Thread t2 = new Thread ( ( ) -> {
for ( int i = 0 ; i< 100 ; i++ ) {
String name = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( name + "==>" + i) ;
}
} , "priority-2" ) ;
t1. setPriority ( 10 ) ;
t2. setPriority ( 1 ) ;
t1. start ( ) ;
t2. start ( ) ;
TimeUnit . SECONDS . sleep ( 2L ) ;
}
}
4.线程的让步
可以通过Thread的静态方法yield,让当前线程从运行状态变为就绪状态 yield方法并不能强制保证CPU不调度自己
package com. ysf ;
import java. util. concurrent. TimeUnit ;
public class Tst04SetThreadYield {
public static void main ( String [ ] args) throws InterruptedException {
Thread t1 = new Thread ( ( ) -> {
for ( int i = 0 ; i< 100 ; i++ ) {
if ( i == 50 ) {
Thread . yield ( ) ;
}
String name = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( name + "==>" + i) ;
}
} , "yield-1" ) ;
t1. start ( ) ;
Thread t2 = new Thread ( ( ) -> {
for ( int i = 0 ; i< 100 ; i++ ) {
String name = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( name + "==>" + i) ;
}
} , "yield-2" ) ;
t2. start ( ) ;
TimeUnit . SECONDS . sleep ( 2L ) ;
}
}
5.线程的休眠
让线程从运行状态变为等待状态 sleep()方法会抛出InterruptedException异常,与结束线程相关
package com. ysf ;
public class Tst05ThreadSleep {
public static void main ( String [ ] args) throws InterruptedException {
System . out. println ( System . currentTimeMillis ( ) ) ;
Thread . sleep ( 1000L ) ;
System . out. println ( System . currentTimeMillis ( ) ) ;
}
}
6.线程的强占
对象方法:join()
既然是一个对象方法,那么就一定需要用线程对象去调用 哪个线程调用了t1.join(),哪个线程就被挂起,直到t1执行完毕后恢复执行 对象方法:join(long mill);
被挂起的线程需要等待t1.join(2000L)执行2秒后恢复执行 如果1秒内t1就执行完了,那么被挂起的线程也会恢复执行
package com. ysf ;
public class Tst06ThreadJoin {
public static void main ( String [ ] args) throws InterruptedException {
Thread t1 = new Thread ( ( ) -> {
String name = Thread . currentThread ( ) . getName ( ) ;
for ( int i = 0 ; i< 10 ; i++ ) {
System . out. println ( name + "==>" + i) ;
}
} , "join-1" ) ;
t1. start ( ) ;
for ( int i = 0 ; i< 5 ; i++ ) {
if ( i == 1 ) {
t1. join ( ) ;
}
System . out. println ( i) ;
}
}
}