欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java入门12(多线程),。。//synchr

来源: javaer 分享于  点击 5024 次 点评:17

Java入门12(多线程),。。//synchr


多线程

线程的实现方式

  1. 继承 Thread 类:一旦继承了 Thread 类,就不能再继承其他类了,可拓展性差
  2. 实现 Runnable 接口:仍然可以继承其他类,可拓展性较好
  3. 使用线程池

继承Thread 类

​ 不能通过线程对象调用 run() 方法,需要通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中

// 这是一个简单的栗子
public class StudentThread extends Thread{
    public StudentThread(String name) {
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 2; i++) {
            System.out.println("This is a test thread!");
            System.out.println(this.getName());
        }
    }
}
// 启动类
public static void main(String[] args) {
    Thread t1 = new StudentThread();
    // 不能通过线程对象调用run()方法
    // 通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中
    t1.start();
}

实现 Runable 接口

​ 实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化

// 介还是一个简单的栗子
public class StudentThreadRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 2; i++) {
            System.out.println("This is a test thread!");
            System.out.println(Thread.currentThread().getName());
        }
    }
}
// 启动类
public static void main(String[] args) {
    // 实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化
    StudentThreadRunnable studentThreadRunnable = new StudentThreadRunnable();
    Thread t01 = new Thread(studentThreadRunnable);
    t01.setName("robot010");
    t01.start();
}

匿名内部类实现

​ 在类中直接书写一个当前类的子类,这个类默认不需要提供名称,类名由JVM临时分配

public static void main(String[] args) {
    Thread t01 = new Thread(){
        @Override
        public void run() {
            for (int i = 0; i < 2; i++) {
                System.out.println("This is a test thread!");
            }
            System.out.println(Thread.currentThread().getName()); // 线程名
            System.out.println(this.getClass().getName()); // 匿名线程类类名
        }
    };
    t01.start();
}

线程的休眠(sleep方法)

​ sleep方法,会使当前线程暂停运行指定时间,单位为毫秒(ms),其他线程可以在sleep时间内,获取JVM的调度资源

// 这是一个计时器
public class TimeCount implements Runnable{
    @Override
    public void run() {
        int count = 0;
        while(true){
            System.out.println(count);
            count++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
// 测试类
public static void main(String[] args) {
    System.out.println("这是main方法运行的时候,开启的主线程~~~");
    TimeCount timeCount = new TimeCount();
    Thread timeThread = new Thread(timeCount);
    System.out.println("开启计时器");
    timeThread.start();

    System.out.println("主线程即将休眠>>>>>>>>>>>");
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println(">>>>>>>>>>>主线程休眠结束~~~~~");
}

线程的加入(join方法)

​ 被 join 的线程会等待 join 的线程运行结束之后,才能继续运行自己的代码

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-01!");
            }
        }
    };
    thread01.start();
    try {
        thread01.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-02!");
            }
        }
    };
    thread02.start();
}
// thread02 会等待 thread01 完全跑完,才会开始自己的线程

线程的优先级(priority方法)

​ 优先级高的线程会有更大的几率竞争到JVM的调度资源,但是高优先级并不代表绝对,充满玄学✨

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-01! " + Thread.currentThread().getPriority());
            }
        }
    };
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-02! " + Thread.currentThread().getPriority());
            }
        }
    };
    thread01.setPriority(1);
    thread02.setPriority(10);
    // 尽管thread02优先级高于thread01,但是也有可能
    thread01.start();
    thread02.start();
}

线程的让步(yield方法)

​ 立刻让出JVM的调度资源,并且重新参与到竞争中

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is thread-01! " + i);
            }
        }
    };
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is thread-02! " + i);
                Thread.yield();
            }
        }
    };
    thread01.start();
    thread02.start();
}

守护线程(Deamon)

​ 会在其他非守护线程都运行结束之后,自身停止运行,(GC垃圾回收机制就是一个典型的守护线程)

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            int times = 0;
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("time pass " + ++times + "second");
            }
        }
    };
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is thread-02! " + i);
            }
        }
    };
    // 将t1设置为守护线程
    thread01.setDaemon(true);
    thread01.start();
    thread02.start();
    // 延长主线程运行,便于观察结果
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    System.out.println("main thread end \\(-_-)/");
}

线程同步

数据操作的原子性

​ 具有原子性的操作,不会被其他线程打断,类似(a++)的操作是不具备原子性的,因此很容易在多线程场景中出现误差

synchronized 悲观锁(互斥性)

优缺点:保证了数据在多线程场景下的安全(保证线程安全),牺牲的是效率,锁的获取和释放,其他线程被阻塞都会额外消耗性能

同步对象:被多个线程所竞争的资源对象叫做同步对象

核心作用: 确保线程在持有锁的期间内,其他线程无法操作和修改指定数据(同步对象)

​ 每一个同步对象都会持有一把线程锁,当线程运行到synchronized 修饰的方法或代码时,线程会自动获取当前同步对象的线程锁,在synchronized 修饰的方法或代码块运行结束后,该线程会自动释放此线程锁,在持有线程锁的这段时间里,其他线程是无法执行synchronized 所修饰的代码块的,其他线程会被阻塞在synchronized 代码块之外,直到这把锁被释放。。。

// synchronized 的两种写法:
// 1. 写在方法之前,修饰整个方法
public synchronized Ticket getTicket(){
    // 取票
    Ticket ticketTmp = null;
    if(!tickets.isEmpty()){
        ticketTmp = tickets.removeLast();
    }
    return ticketTmp;
}
// 2. 代码块,修饰代码块所包含的部分
public Ticket getTicket(){
    // 取票
    synchronized(this){
        Ticket ticketTmp = null;
        if(!tickets.isEmpty()){
            ticketTmp = tickets.removeLast();
        }
        return ticketTmp;
    }
}

线程死锁

相关栏目:

用户点评