多线程的常用操作方法

1.线程的命名与取得

所有线程程序的执行,会根据自己的情况进行资源抢占。要区分线程,就要依靠线程的名字。

对于线程的名字,一般而言,会在其启动之前进行定义。不建议对已启动的线程进行命名或修改。也不建议命名重复。

如果想要进行线程名称的操作,Thread 有如下方法:

  • 构造方法:

    public Thread(Runnable target, String name){}
    

    其他也有很多构造方法包含 name 参数进行命名。

  • 通过 set 方法设置名字

    public final String getName() {
            return name;
    }
    
  • 通过 get 方法取得名字

    public final String getName() {
    	return name;
    }
    

对于线程名字操作有个问题,这些方法是属于 Thread 的,使用 Runnable 的话。

那么能够取得的就是当前执行本方法的线程名字。

所以 Thread 中有一个取得当前线程的方法。

  • public static native Thread currentThread();
    

如果在实例化 Thread 对象的时候没有命名,那么会自动进行编号命名,保证对象不重复。

class MyThread implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread mt = new MyThread();
        new Thread(mt).start();
        new Thread(mt).start();
        new Thread(mt).start();
    }
}


out:
Thread-0
Thread-1
Thread-2

Process finished with exit code 0

对于直接调用 run 方法有如下情况:

class MyThread implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread mt = new MyThread();
        new Thread(mt,"A").start();
        mt.run();
    }
}

out:
main
A

Process finished with exit code 0

run 方法打印了 main,即 main 线程。

所有在 main 方法上创建的线程实例都可以将其表示为子线程。

线程一直存在,主方法就是主线程。

进程是在用 java 命令解释一个程序类的时候启动的。

对于操作系统而言,都相当于启动了一个新的进程,main 只是这个新进程中的子线程。

每个 JVM 进程启动的时候至少启动几个线程?

  • main 线程:程序的主要执行,以及启动子线程;
  • gc 线程:进行垃圾处理;

2.线程的休眠

让线程的执行速度稍微变慢一点。

休眠的方法:

public static void sleep(long millis) throws InterruptedException{}

默认情况下,如果休眠的时候如果设置了多个线程对象,那么所有线程将“一起”进入 run()方法。

因为先后顺序太短了,实际上是几微秒到几毫秒的区别。

3.线程的优先级

所谓的优先级指的是越高的优先级,越有可能先执行。在 Thread 中有两个方法进行优先级操作。

  • 设置优先级:

    public final void setPriority(int newPriority) { }
    
  • 取得优先级:

    public final int getPriority() { }
    

参数是 int 类型。对于此内容有三种取值

public final static int MIN_PRIORITY = 1;

    public final static int NORM_PRIORITY = 5; //默认

    public final static int MAX_PRIORITY = 10;

主线程的优先级是?

​ 主线程属于中等优先级 ( 5 )

4.线程的同步与死锁

  1. 线程的同步产生的原因

  2. 线程的同步处理操作

  3. 线程的死锁情况

同步:

多个线程访问同一资源时,资源的重复读的类似的问题。

class MyThread implements Runnable{
    private int ticket = 5;
    @Override
    public void run() {
        for (int i = 0 ; i < 20 ; i++){
            if (this.ticket > 0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "卖票 , ticket = "+ticket--);
            }
        }
    }
}

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread.currentThread().getPriority();
        MyThread mt = new MyThread();
        Thread t1 = new Thread(mt,"A");
        Thread t2 = new Thread(mt,"B");
        Thread t3 = new Thread(mt,"C");
        t1.start();
        t2.start();
        t3.start();
    }
}


out:
A卖票 , ticket = 4
B卖票 , ticket = 4
C卖票 , ticket = 5
A卖票 , ticket = 3
B卖票 , ticket = 2
C卖票 , ticket = 3
C卖票 , ticket = 1
B卖票 , ticket = 0
A卖票 , ticket = -1

Process finished with exit code 0

出现了负数,这是因为出现了延迟,数据重复读了。

同步操作

​ 问题就出现在判断和修改数据是分开完成的。

可以使用 synchronized 关键字实现同步。有两种方法:

  1. 同步代码块
  2. 同步方法
class MyThread implements Runnable{
    private int ticket = 5;
    @Override
    public void run() {
        for (int i = 0 ; i < 20 ; i++){
            synchronized (this){	//同步代码块
                if (this.ticket > 0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖票 , ticket = "+ticket--);
                }
            }
        }
    }
}
class MyThread implements Runnable {
    private int ticket = 20;
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            this.sale();
        }
    }
    public synchronized void sale() {
        if (this.ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖票 , ticket = " + ticket--);
        }
    }
}

推荐使用同步方法。

多个线程访问同一个资源的时候一定要使用同步。(同步的是数据)

死锁

所谓的同步就是一个对象等待另一个线程对象执行完毕的操作形式。

线程同步过多有可能造成死锁。

死锁产生的情况:

我说:你给我本子我就给你笔,

你说:你给我笔我就给你本子。

双方都等不到,都占用着,逻辑错误!