Java多线程相关知识【15】--设计模式--确保挂起模式(Guarded Suspension)

文章目录

  • Java多线程相关知识【15】--设计模式--确保挂起模式(Guarded Suspension)
    • 1.问题的引入
    • 2.问题的解决
    • 3.实现的代码
      • 请求队列
      • 请求体
      • 连接端
      • 服务端
      • 使用方法

菜鸟的一个学习笔记,欢迎大神 批评指正

Java多线程相关知识【15】–设计模式–确保挂起模式(Guarded Suspension)

1.问题的引入

​ 一个线程正在做一个非常关键的任务,这时,有一个其他的线程让当前线程做其他的事情,当前线程只有完成当前的任务才能做其他的任务。

2.问题的解决

​ 使用队列将其他需要工作的线程保存,然后在未来需要时,继续取队列中获取任务,然后执行。

3.实现的代码

请求队列

package com.company.ThreadCharPart2.guardedsuspension;

import java.util.LinkedList;

/**
 * 请求队列
 */
public class GuardedSuspensionQueue {
     
    /**
     * 队列
     */
    LinkedList<GuardedSuspension> queue = new LinkedList<>();

    /**
     * 获取队列的元素
     *
     * @return
     */
    public GuardedSuspension getGuardedSuspension() {
     
        synchronized (queue) {
     
            while (queue.size() <= 0) {
     
                try {
     
                    queue.wait();
                } catch (InterruptedException e) {
     
                    return null;
                }
            }
            return queue.removeFirst();
        }
    }

    /**
     * 向队列中添加元素
     *
     * @param guardedSuspension 要添加的元素
     */
    public void putGuardedSuspension(GuardedSuspension guardedSuspension) {
     
        synchronized (queue) {
     
            queue.addLast(guardedSuspension);
            queue.notifyAll();
        }
    }
}

请求体

/**
 * 请求元素体
 */
public class GuardedSuspension<T> {
     
    /**
     * 请求元素的类型
     */
    final private T value;

    public GuardedSuspension(T value) {
     
        this.value = value;
    }

    /**
     * 获取元素
     *
     * @return
     */
    public T getValue() {
     
        return value;
    }
}

连接端

/**
 * 客户端操作
 * @param 
 */
public class GuardedSuspensionClientThread<T> extends Thread {
     
    private final GuardedSuspensionQueue queue;
    private final T value;
    private final Random random;

    public GuardedSuspensionClientThread(GuardedSuspensionQueue queue, T value) {
     
        this.queue = queue;
        this.value = value;
        this.random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
     
        for (int i = 0; i < 10; i++) {
     
            System.out.println("This client value ->"+value);
            queue.putGuardedSuspension(new GuardedSuspension(value));
            try {
     
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
     
                e.printStackTrace();
            }
        }
    }
}

服务端

/**
 * 服务端
 */
public class GuardedSuspensionServerThread extends Thread {
     
    private final GuardedSuspensionQueue queue;
    private final Random random;
    //标识是否确认关闭
    private volatile boolean close = false;


    public GuardedSuspensionServerThread(GuardedSuspensionQueue queue) {
     
        this.queue = queue;
        random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
     
        while (!close) {
     
            GuardedSuspension suspension = queue.getGuardedSuspension();
            if (suspension == null) {
     
                System.out.println("error");
                continue;
            }
            System.out.println("Server ->" + suspension.getValue());
            try {
     
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
     
                return;
            }
        }
    }

    /**
     * 关闭服务端
     */
    public void close() {
     
        this.close = true;
        this.interrupt();
    }
}

使用方法

/**
 * 测试连接
 */
public class GuardedSuspensionClient {
     
    public static void main(String[] args) throws InterruptedException {
     
        //初始化队列
        final GuardedSuspensionQueue queue = new GuardedSuspensionQueue();
        //客户端初始化并启动
        new GuardedSuspensionClientThread(queue, "test").start();
        //服务端操作
        GuardedSuspensionServerThread guardedSuspensionServerThread = new GuardedSuspensionServerThread(queue);
        guardedSuspensionServerThread.start();
        Thread.sleep(11000L);
        guardedSuspensionServerThread.close();
    }
}

你可能感兴趣的:(#,Java多线程,Java,Java,多线程,设计模式,确保挂起模式,Guarded,Suspension)