Thread及Object中的一些方法

wait、notify

wait和notify方法是Obejct类中的方法,相当于所有的对象均有这两个方法。当一个线程持有了对象object锁使用权之后才能使用object的wait、notify方法,因此需要将方法调用放在synchronized同步方法或代码块中,保证当前线程持有了object锁的使用权,若线程没有持有对象obejct锁使用权,调用了object的wait、notify方法则会抛出 java.lang.IllegalMonitorStateException

  • wait() 线程会释放当前持有的object对象锁,进入等待队列之中,等待被notify或notifyAll方法唤醒,之后再继续执行。wait方法与sleep方法相比,Thread.sleep(Long millis)为静态方法,会导致调用的线程进入睡眠状态,wait方法需通过持有锁的对象调用,且要在synchronized代码块或方法中,此外sleep方法不会释放锁
  • notify() 随机唤醒一个正在等待当前对象的线程,但并不能保证被唤醒的线程能立即获取锁,如果调用notify的线程持有锁的使用权,那么必须等当前线程执行完毕,释放锁后,被唤醒的线程才能竞争锁的使用权
  • notifyAll:唤醒所有正在wait当前对象的线程,但是被唤醒的线程会再次去竞争对象锁。因为一次只有一个线程能拿到锁,所有其他没有拿到锁的线程会被阻塞
public class TestWaitAndNotify {

    public static void main(String[] args) {
        Object o = new Object();
        Thread_A thread_a = new Thread_A(o, "thread_A");
        Thread_B thread_b = new Thread_B(o, "thread_B");
        thread_a.start();
        thread_b.start();
    }

    public static class Thread_A extends Thread {

        private Object o;

        public Thread_A(Object o, String name) {
            super(name);
            this.o = o;
        }

        @Override
        public void run() {
            try {
                synchronized (o) {
                    System.out.println(Thread.currentThread().getName() + "======start");
                    System.out.println(Thread.currentThread().getName() + "======wait");
                    o.wait();
                    System.out.println(Thread.currentThread().getName() + "======end");
                }
            } catch (Exception e) {

            }
        }
    }

    public static class Thread_B extends Thread {
        private Object o;
        public Thread_B(Object o, String name) {
            super(name);
            this.o = o;
        }

        @Override
        public void run() {
            try {
                synchronized (o) {
                    System.out.println(Thread.currentThread().getName() + "======start");
                    System.out.println(Thread.currentThread().getName() + "======notify");
                    o.notify();
                    System.out.println(Thread.currentThread().getName() + "======end");
                }
            } catch (Exception e) {}
        }
    }
}

输出:
thread_A======start
thread_A======wait
thread_B======start
thread_B======notify
thread_B======end
thread_A======end

Java中wait和notify的简单使用
Java中wait、notify、notifyAll使用详解

interrupt

Thread类中有interrupt()、interrupted()、isInterrupted()等方法

  • interrupt()当线程对象调用interrupt方法后,表示线程被中断,但只是将线程的中断标志置为true,仅此而已,线程依然可以正常运行。
  • interrupted(),判断当前调用线程是否被置为中断状态,它是一个静态方法,如果被置为中断则返回true,并将中断状态复原。实际使用的是私有方法isInterrupted(boolean ClearInterrupted),当线程被标记中断时,根据ClearInterrupted参数,判断是否将中断标记清除
public static boolean interrupted() {
   return currentThread().isInterrupted(true);
}
private native boolean isInterrupted(boolean ClearInterrupted);
  • isInterrupted(),与interrupted()方法功能类似,判断线程是否被中断,不同的是interrupted()方法判断的是当前的调用线程,isInterrupted()判断的是方法所属的线程;且当线程被标为中断时,isInterrupted()并不会清除标记
public boolean isInterrupted() {
        return isInterrupted(false);
}

代码示例如下:

public class Test {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().isInterrupted());
            Thread.currentThread().interrupt();
            //不会将标志清楚
            System.out.println(Thread.currentThread().isInterrupted());
            //会将标记清楚
            System.out.println(Thread.interrupted());
            System.out.println(Thread.currentThread().isInterrupted());
        });
        thread.start();
    }
}
阻塞与线程中断

当处于阻塞状态的线程如调用了Object.wait()、Condition.await()、Thread.sleep()等方法,被置为中断状态后,会抛出InterruptedException,且中断状态会被清除

public class 阻塞与中断 {
   public static void main(String[] args) {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "中断状态为" + Thread.interrupted());
            try {
                synchronized (o) {
                    System.out.println("进入同步代码块");
                    o.wait();
                }
            } catch (Exception e) {
                System.out.println(e);
                System.out.println(Thread.currentThread().getName() + "中断状态为" + Thread.interrupted());
            }
        });

        thread.start();
        try {
            Thread.sleep(1000);
            System.out.println("当前thread中断状态"+thread.isInterrupted());
            thread.interrupt();
        } catch (Exception e) {}
    }
}

执行结果:
Thread-0中断状态为false
进入同步代码块
当前thread中断状态false
java.lang.InterruptedException
Thread-0中断状态为false

JAVA并发编程(八)之interrupt方法

CompletionService

  • java.util.concurrent.CompletionService

当我们使用线程池提交一批任务后,会获取一批Future对象集合,可以通过Future的get()方法获取任务的执行结果,但是get方法是阻塞的,当我们调用了一个Futrue对象的get方法后,可能会长时间阻塞,在这一过程中其他任务执行完毕,无法及时获取并处理已正常返回的执行结果。

CompletionService解决了这个问题,它包含有一个BlockingQueue<Future> completionQueue;当提交任务之后,任务会按照完成顺序,将执行结果放入队列之中,调用take方法获取Future对象,就可以按照任务完成顺序依次获取执行结果。

CompletionService有一个实现类ExecutorCompletionService,创建该对象时需要先传入一个Executor对象,即一个线程池。

import java.util.Random;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class 多线程测试 {
    public static void main(String[] args) throws Exception {
        多线程测试 my = new 多线程测试();
        System.out.println(my.getNumGreatThan20());
    }

    public Boolean getNumGreatThan20() throws Exception {
        CompletionService<Integer> cs = new ExecutorCompletionService<>(Executors.newCachedThreadPool());
        cs.submit(() -> aMethod());
        cs.submit(() -> aMethod());
        cs.submit(() -> aMethod());
        for (int i=0; i<3; i++) {
            if (cs.take().get() > 10) {
                return true;
            }
        }
        return false;
    }

    public int aMethod() {
        Random random = new Random();
        int n = random.nextInt(25);
        System.out.println(n);
        return n;
    }
}

CompletionService介绍

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值