线程的命名与取得
构造方法:public Thread(Runnable target , String name);
设置名字:public final void setName(String name);
取得名字:public final String getName();
范例:
class MyThread implements Runnable {
System.out. println (Thread. currentThread (). getName ());
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
MyThread mt = new MyThread ();
new Thread (mt, " 线程A " ). start ();
new Thread (mt, " 线程B " ). start ();
线程的休眠
休眠:public static void sleep(long millis ) throws InterruptedException;
休眠:public static void sleep(long millis, int nanos ) throws InterruptedException;
在进行休眠的时候可能会产生中断异常“InterruptedException”,中断异常属于Exception的子类必须处理。
范例:
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
for ( int x = 0 ; x < 10 ; x ++ ){
System.out. println (Thread. currentThread (). getName () + " 、x = " + x);
Thread. sleep ( 1000 ); //暂缓执行
// TODO Auto-generated catch block
线程中断
在之前发现线程休眠提供有一个中断异常,实际上就证明线程的休眠是可以被打断的。 在Thread类里面有提供这种中断执行的处理方法:
判断线程是否被中断:public boolean isInterrupteed();
中断线程执行:public void interrupt();
范例:
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
Thread thread = new Thread (() -> {
System.out. println ( " 我要睡觉了 " );
System.out. println ( " 睡够了 " );
// TODO Auto-generated catch block
System.out. println ( " 敢打扰我睡觉,老子宰了你 " );
if ( ! thread. isInterrupted ()){
System.out. println ( " 打扰一下你睡觉 " );
所有执行的线程都是可以被中断的,中断线程必须进行异常的处理。
线程的强制执行:
所为的线程的强制执行指的是当满足某些条件之后,某一个线程对象将可以一直独占资源,一直到该线程的程序执行结束。
范例:观察一个没有强制执行的程序
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
Thread thread = new Thread (() -> {
for ( int x = 0 ; x < 100 ; x ++ ) {
System.out. println (Thread. currentThread (). getName () + " 执行、 x = " + x);
for ( int x = 0 ; x < 100 ; x ++ ){
System.out. println ( " [霸道的main线程]number = " + x);
这个时候主线程和子线程都在交替执行,一直抢占资源。如果希望主程序独占执行,那么就可以利用:
强制执行:public final void join() throws InterruptedException;
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
Thread mainThread = Thread. currentThread (); //获得主线程
Thread thread = new Thread (() -> {
for ( int x = 0 ; x < 100 ; x ++ ) {
if (x == 3 ){ // 如果执行到等于3 了,则霸道线程加入
// TODO Auto-generated catch block
System.out. println (Thread. currentThread (). getName () + " 执行、 x = " + x);
for ( int x = 0 ; x < 100 ; x ++ ){
System.out. println ( " [霸道的main线程]number = " + x);
线程的礼让:线程的礼让指的是先将资源让出去让别的线程先执行。线程的礼让可以使用Thread中提供的方法:
礼让:public static void yield();
范例:礼让操作
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
Thread mainThread = Thread. currentThread (); //获得主线程
Thread thread = new Thread (() -> {
for ( int x = 0 ; x < 100 ; x ++ ) {
System.out. println ( " ####线程的礼让操作#### " );
System.out. println (Thread. currentThread (). getName () + " 执行、 x = " + x);
for ( int x = 0 ; x < 100 ; x ++ ){
System.out. println ( " [霸道的main线程]number = " + x);
每次调用yeild()方法只会礼让一次。
线程的优先级:从理论上来讲,线程的优先级越高就越有可能先执行。在Thread类里面针对优先级操作有两个处理方法
设置优先级
final void setPriority(int newPriority)
获取优先级
final int getPriority()
在进行优先级定义的时候都是通过int型的数字来完成的,而对于此数字的选择在Thread类里面就定义有三个常量:
最高优先级:public static final int MAX_PRIORITY;(10)
中等优先级:public static final int NORM_PRIORITY;(5)
最低优先级:public static final int MIN_PRIORITY;(1)
public class ThreadDemo {
public static void main ( String [] args ) throws Exception {
for ( int x = 0 ; x < 5 ; x ++ ){
// TODO Auto-generated catch block
System.out. println (Thread. currentThread (). getName () + " 执行 " );
Thread threadA = new Thread (run , " 线程对象A " );
Thread threadB = new Thread (run , " 线程对象B " );
Thread threadC = new Thread (run , " 线程对象C " );
优先级高的有可能限制性,而不是绝对先执行