Java操作的相关方法

1.取得和设置线程的名称(如果没有为线程设置名称,则系统会自动为线程分配名称Thread-Xx)

例如:

class MyThread5 implements Runnable{
    public void run(){
        for(int i = 0;i<3;i++){
            System.out.println(Thread.currentThread().getName()+"运行"+i);//取得当前线程名称
        }
    }
}
public class ThreadNameDemo {
    public static void main(String[] args) {
        MyThread5 my = new MyThread5();
        new Thread(my).start();
        new Thread(my,"线程A").start();
        new Thread(my,"线程B").start();
        new Thread(my).start();
        new Thread(my).start();
    }
}
运行结果如下:未被命名的线程被自动命名
java程序每次运行至少启动两个线程,一个是main线程,一个是垃圾收集线程
例如:
class MyThread5 implements Runnable{
    public void run(){
        for(int i = 0;i<3;i++){
            System.out.println(Thread.currentThread().getName()+"运行"+i);
        }
    }
}

public class CurrentThreadDemo {
    public static void main(String[] args) {
        MyThread5 my = new MyThread5();
        new Thread(my,"线程").start();
        my.run();
    }
}

运行结果为:哪个线程先抢到CPU资源,哪个线程就先运行

2.判断线程是否启动(isAlive()方法)

例如:

public class ThreadAliveDemo {
    public static void main(String[] args) {
        MyThread6 mt = new MyThread6();
        Thread t = new Thread(mt,"线程");
        System.out.println("线程开始执行之前"+t.isAlive());
        t.start();//启动线程
        System.out.println("线程开始执行之后"+t.isAlive());
        for(int i = 0;i<3;i++){
            System.out.println("main运行"+i);
        }
        System.out.println("代码执行之后"+t.isAlive());
    }
}

运行结果:(运行结果不唯一,主线程有可能比其他线程先执行完)

3.线程的强制运行(使用join()方法)

ps:强制运行的话要加上try...catch语句

4.线程的休眠(使用Thread.sleep()方法,单位为毫秒)

class MyThread9 implements Runnable {
    public void run(){
        for(int i = 0;i<3;i++){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"在运行");
        }
    }
}

线程每隔2秒输出一次

5.中断线程

public class MyThread8 implements Runnable {
    public void run(){
        System.out.println("1进入run方法");
        try {
            Thread.sleep(10000);
            System.out.println("2.已经完成休眠");
        } catch (InterruptedException e) {//休眠中断就执行catch语句
            System.out.println("3休眠停止");
            return;//让程序返回被调用处
        }
        System.out.println("4");
    }
}
 
public class ThreadInterruptDemo {
    public static void main(String[] args) throws InterruptedException {
        MyThread8 my = new MyThread8();
        Thread t = new Thread(my,"线程");
        t.start();
        Thread.sleep(2000);//稍微停2秒后继续中断
        t.interrupt();

    }
}

运行结果:

在进行10秒休眠状态的第二秒休眠终止,执行catch语句返回程序调用处
6.后台线程(使用setDaemon()方法)设置后台线程的话,即使Java进程结束了,后台线程依然会执行
例如
public class ThreadDaemonDemo4 {
    public static void main(String[] args) {
        MyThread10 my = new MyThread10();
        Thread t = new Thread(my);
        t.setDaemon(true);//设置此线程在后台运行
   t.start();//启动线程
 }}
class MyThread10 implements Runnable {
     public void run(){
         while(true){
             System.out.println(Thread.currentThread().getName()+"在运行");
         }
     }
}
7.线程优先级(用setPriority来设置一个线程的优先级,用getPriority来取得一个线程的优先级)
哪个线程优先级高,哪个线程就 有可能会先被执行
例如:
class MyThread9 implements Runnable {
    public void run(){
        for(int i = 0;i<3;i++){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"在运行");
        }
    }
}
public class ThreadPirorityDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread9(),"A");
        Thread t2 = new Thread(new MyThread9(),"B");
        Thread t3 = new Thread(new MyThread9(),"C");
        t1.setPriority(10);//也可以换成t1.setPriority(Thread.MAX_PRIORITY)
        t2.setPriority(5);
        t3.setPriority(1);
        t1.start();
        t2.start();
        t3.start();
    }
}
PS:主线程的优先级是NORM_PRIORITY(5)
8.线程的礼让(使用yield()方法):将一个线程的操作暂时让给其他线程
用法:Thread.currentThread().yield();







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值