多线程学习Day03-加餐

本文介绍了Java中的同步模式GuardedSuspension,如何在多线程间通过GuardedObject传递结果,以及其在生产者消费者模式、数据库查询和文件读写等场景的应用。还讨论了join方法的原理和使用场景,以及消息队列在平衡生产者和消费者之间的资源消耗。
摘要由CSDN通过智能技术生成

同步模式之保护性暂停

.即 Guarded Suspension,用在一个线程等待另一个线程的执行结果

要点 :有一个结果需要从一个线程传递到另一个线程,让他们关联同一个 GuardedObject

如果有结果不断从一个线程到另一个线程那么可以使用消息队列(见生产者/消费者)

JDK 中,join 的实现、Future 的实现,采用的就是此模式

因为要等待另一方的结果,因此归类到同步模式

     

class GuardedObject{
    private Object response;
    //获取结果
    public Object get() throws InterruptedException {
        synchronized (this){
            while(response==null){
                this.wait();
            }
            return response;
        }
    }
    public void complete(Object response){
        synchronized (this){
            //结果变量赋值
            this.response=response;
            this.notifyAll();
        }
    }
}


public static void main(String[] args) {
 GuardedObject guardedObject = new GuardedObject();
 new Thread(() -> {
try {
// 子线程执行下载
List<String> response = download();
log.debug("download complete...");
guardedObject.complete(response);
} catch (IOException e) {
e.printStackTrace();
}
 }).start();
 log.debug("waiting...");
 // 主线程阻塞等待
 Object response = guardedObject.get();
 log.debug("get response: [{}] lines", ((List<String>) response).size());
}

下面这个例子感觉更好一些,这里最多等5秒,超过抛出异常

public class Test15 {
    public static void main(String[] args) {
        GuardedObject guardedObject = new GuardedObject();

        // Thread to wait for the response
        Thread waitingThread = new Thread(() -> {
            System.out.println("Waiting for the response...");
            Object response = guardedObject.get(5000);
            System.out.println("Response received: " + response);
        });

        // Thread to provide the response
        Thread notifyingThread = new Thread(() -> {
            try {
                Thread.sleep(3000); // Simulate some delay
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            guardedObject.complete("Hello World!");
        });

        waitingThread.start();
        notifyingThread.start();
    }
}
class GuardedObject {
    private Object response;

    public synchronized Object get(long timeout) {
        long startTime = System.currentTimeMillis();
        while (response == null) {
            long passedTime = System.currentTimeMillis() - startTime;
            long remainingTime = timeout - passedTime;
            if (remainingTime <= 0) {
                throw new RuntimeException("Timeout waiting for response");
            }
            try {
                wait(remainingTime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return response;
    }

    public synchronized void complete(Object r) {
        this.response = r;
        notifyAll();
    }
}

保护性暂停模式常用于以下场景:

  • 实现生产者消费者模式中的缓冲区。
  • 等待异步操作完成,如数据库查询、文件读写等。
  • 控制资源访问,确保线程安全。

这种模式通过简化线程间的协作和通信,能有效解决多线程程序中的复杂同步问题。

join()的原理

是调用者轮询检查线程 alive 状态

t1.join();等价于下面的代码

synchronized (t1) {
 // 调用者线程进入 t1 的 waitSet 等待, 直到 t1 运行结束
 while (t1.isAlive()) {
 t1.wait(0);
 }
}

多任务版GuardedObject

       

一个送信的例子,写了我好久啊

@Slf4j(topic = "c.Test16")
public class Test16 {
    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<3;i++){
            new People().start();
        }
        Thread.sleep(1000);
        for(Integer id:Mailboxes.getIds()){
            new Postman(id,"内容"+id).start();
        }
    }

}
@Slf4j(topic = "c.Test16")
class Postman extends Thread{
    private int id;
    private String mail;

    public Postman(int id,String mail){
        this.id=id;
        this.mail=mail;
    }
    @Override
    public void run(){
       GuardObject_16 guardObject16=Mailboxes.getGuardObject_16(id);
       guardObject16.complete(mail);
       log.debug("开始送信 id:{},内容:{}",id,mail);
    }
}
@Slf4j(topic = "c.Test16")
class People extends Thread{
    @Override
    public void run(){
        GuardObject_16 guardObject16=Mailboxes.createGuardedObject();

        log.debug("开始收信id:{}",guardObject16.getId());
        Object mail=guardObject16.get(5000);
        log.debug("收到信id:{},内容",guardObject16.getId(),mail);
    }
}
class Mailboxes{
    private static Map<Integer,GuardObject_16> boxes=new Hashtable<>();
    private static int id=1;
    private static synchronized int generateId(){
        return id++;
    }

    public static GuardObject_16 createGuardedObject(){
        GuardObject_16 go =new GuardObject_16(generateId());
        boxes.put(go.getId(), go);
        return go;
    }
    public static Set<Integer> getIds(){
        return boxes.keySet();
    }
    public static GuardObject_16 getGuardObject_16(int id){
        return boxes.remove(id);
    }
}
class GuardObject_16{
    private int id;
    public GuardObject_16(int id){
        this.id=id;
    }

    public int getId() {
        return id;
    }
    private Object response;
    public Object get(long timeout) {
        synchronized (this) {
            // 开始时间 15:00:00
            long begin = System.currentTimeMillis();
            // 经历的时间
            long passedTime = 0;

            while (response == null) {
                // 这一轮循环应该等待的时间
                long waitTime = timeout - passedTime;
                // 经历的时间超过了最大等待时间时,退出循环
                if (timeout - passedTime <= 0) {
                    break;
                }
                try {
                    this.wait(waitTime); // 虚假唤醒 15:00:01
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 求得经历时间
                passedTime = System.currentTimeMillis() - begin; // 15:00:02 1s
            }
            return response;
        }
    }
    // 产生结果
    public void complete(Object response) {
        synchronized (this) {
            // 给结果成员变量赋值
            this.response = response;
            this.notifyAll();
        }
    }
}
21:01:27 [Thread-2] c.Test16 - 开始收信id:3
21:01:27 [Thread-0] c.Test16 - 开始收信id:1
21:01:27 [Thread-1] c.Test16 - 开始收信id:2
21:01:28 [Thread-1] c.Test16 - 收到信id:2,内容
21:01:28 [Thread-4] c.Test16 - 开始送信 id:2,内容:内容2
21:01:28 [Thread-0] c.Test16 - 收到信id:1,内容
21:01:28 [Thread-3] c.Test16 - 开始送信 id:3,内容:内容3
21:01:28 [Thread-2] c.Test16 - 收到信id:3,内容
21:01:28 [Thread-5] c.Test16 - 开始送信 id:1,内容:内容1

异步模式之生产者消费者

与前面的保护性暂停中的 GuardObject 不同,不需要产生结果和消费结果的线程一一对应

消费队列可以用来平衡生产和消费的线程资源

生产者仅负责产生结果数据,不关心数据该如何处理,而消费者专心处理结果数据

消息队列是有容量限制的,满时不会再加入数据,空时不会再消耗数据

JDK 中各种阻塞队列,采用的就是这种模式

     

示例代码(又搞了半天)

@Slf4j(topic = "c.Test17")
public class Test17 {
    public static void main(String[] args) {
        MessageQueue queue=new MessageQueue(2);
        for(int i=0;i<3;i++){
            int id=i;
            new Thread(()->{
                try {
                    queue.put(new Message(id,"值"+id));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            },"生产者"+i).start();
        }
        new Thread(()->{
            while(true){
                try {
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Message message= queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        },"消费者").start();
    }
}
@Slf4j(topic = "c.Test17")
//消息队列类
class MessageQueue{
    //消息的队列集合
    private LinkedList<Message> list=new LinkedList<>();
    //队列容量
    private int capcity;

    public MessageQueue(int capcity) {
        this.capcity = capcity;
    }

    //获取消息
    public Message take() throws InterruptedException {
       //检查队列是否为空
        synchronized (list){
            while(list.isEmpty()){
                log.debug("队列为空,消费者线程等待");
                list.wait();
            }
            Message message= list.removeFirst();
            log.debug("已消费消息{}",message);
            //唤醒一下子
            list.notifyAll();
            return message;
        }
    }
    //存入消息
    public void put(Message message) throws InterruptedException {
        synchronized (list){
            while(list.size()==capcity){
                log.debug("队列满了,生产者线程等待");
                list.wait();
            }
            //消息放入队列尾部
            list.addLast(message);
            log.debug("已生产消息{}",message);
            list.notifyAll();
        }
    }
}

final class Message{
    private int id;
    private Object value;

    public Message(int id,Object value) {
        this.id = id;
        this.value=value;
    }
    public int getId() {
        return id;
    }

    public Object getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Message{" +
                "id=" + id +
                ", value=" + value +
                '}';
    }
}

结果

21:41:39 [生产者0] c.Test17 - 已生产消息Message{id=0, value=值0}
21:41:39 [生产者1] c.Test17 - 已生产消息Message{id=1, value=值1}
21:41:39 [生产者2] c.Test17 - 队列满了,生产者线程等待
21:41:40 [消费者] c.Test17 - 已消费消息Message{id=0, value=值0}
21:41:40 [生产者2] c.Test17 - 已生产消息Message{id=2, value=值2}
21:41:41 [消费者] c.Test17 - 已消费消息Message{id=1, value=值1}
21:41:42 [消费者] c.Test17 - 已消费消息Message{id=2, value=值2}
21:41:43 [消费者] c.Test17 - 队列为空,消费者线程等待

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值