线程基本操作

命名

定义

public final String getName();//返回此线程的名称。

栗子

package com.test;

class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "  线程运行 ...");
    }
}
public class JavaDemo {
    public static void main(String[] args) throws Exception {
        MyThread threadBody = new MyThread() ;
        new Thread(threadBody).start();//未命名的线程
        new Thread(threadBody,"线程 - A").start();//已命名的线程
        new Thread(threadBody).start();//未命名的线程
        new Thread(threadBody,"线程 - B").start();//已命名的线程
    }
}

运行结果:
结果
此时,已命名的线程使用自定义的名称,而未命名的线程使用程序定义的不重复的名称。

休眠

定义

  1. 当前正在执行的线程休眠(暂时停止执行)指定的毫秒数
public static void sleep​(long millis) throws InterruptedException;
  1. 当前正在执行的线程休眠(暂时停止执行)指定的毫秒数加上指定的纳秒数
public static void sleep​(long millis, int nanos) throws InterruptedException;

栗子

package com.test;

class MyThread implements Runnable {
    @Override
    public void run() {
        for (int x = 0 ; x < 5 ; x ++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行,x = " + x);
        }
    }
}
public class JavaDemo {
    public static void main(String[] args) throws Exception {
        MyThread body = new MyThread() ;
        new Thread(body,"运行线程").start();
        body.run() ;
    }
}

中断

定义

  1. 测试此线程是否已被中断
public boolean isInterrupted();
  1. 中断此线程
public void interrup();

栗子

package com.test;

public class JavaDemo {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(()->{
            try {
                Thread.sleep(20000);
                System.out.println("吃饱睡足,开始学习");
            } catch (InterruptedException e) {
                System.out.println("休息被中断了,于是开始xuexi");
            }
        }) ; 
        thread.start();
        System.out.println(thread.getName() + "线程的状态:" + thread.isInterrupted());
        Thread.sleep(2000);
        thread.interrupt(); // 由主线程中断了一个子线程
        System.out.println(thread.getName() + "线程的状态:" + thread.isInterrupted());
    }
}

运行结果:
结果

强制运行

定义

public final void join() throws InterruptedException;

栗子

public class JavaDemo {
    public static void main(String[] args) throws Exception {
        Thread mainThread = Thread.currentThread() ; // 获得主线程对象
        Thread thread = new Thread(()->{
            for (int x = 0 ; x < 10 ; x ++) {
                if (x == 2) {  // 设置一个强制的触发条件
                    try {
                        mainThread.join(); // 主线程强制执行
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
            }
        },"循环线程") ;
        thread.start();
        for (int x = 0 ; x < 5 ; x ++) {
            Thread.sleep(100);  // 延缓程序的执行速度
            System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
        }
    }
}

运行结果:
结果
感觉就是等待“ mainThread ”线程死亡。

礼让

定义

public static void yield();

栗子

public class JavaDemo {
    public static void main(String[] args) throws Exception {
        Thread mainThread = Thread.currentThread() ; // 获得主线程对象
        Thread thread = new Thread(()->{
            for (int x = 0 ; x < 10 ; x ++) {
                if (x % 2 == 0) {
                    Thread.yield(); // 礼让一次
                    System.out.println("线程礼让," + Thread.currentThread().getName());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
            }
        },"循环线程") ;
        thread.start();
        for (int x = 0 ; x < 10 ; x ++) {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
        }
    }
}

运行结果:
结果

优先级

定义

优先级定义

  1. MAX_PRIORITY 最大优先级
  2. MIN_PRIORITY 最小优先级
  3. NORM_PRIORITY 默认优先级(中等)

栗子

package com.test;

public class JavaDemo {
    public static void main(String[] args) throws Exception {
        Runnable run = ()->{
            for (int x = 0 ; x < 5 ; x ++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "运行,x = " + x);
            }
        } ;
        Thread threadA = new Thread(run,"线程1") ;
        Thread threadB = new Thread(run,"线程2") ;
        Thread threadC = new Thread(run,"线程3") ;
        threadA.setPriority(Thread.MIN_PRIORITY);
        threadB.setPriority(Thread.MIN_PRIORITY);
        threadC.setPriority(Thread.MAX_PRIORITY);
        threadA.start();
        threadB.start();
        threadC.start();
    }
}

运行结果:
结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值