目录
定义:
/** * 活动生物类是活动对象示例的基础。 * <p> * 对于每个驻留在其控制线程中的对象,活动对象设计模式将方法执行与方法调用分离。目标是通过使用异步方法调用引入并发性,并使用调度器处理请求。 * <p> * 实现活动对象模式的类将包含自同步机制,而不使用“synchronized”方法 * <p> * 要实现一个拥有自己的控制线程机制的生物,并且只公开它的API而不公开执行本身,我们可以使用活动对象模式。 */
场景:
考虑一个对象要读取操作文件,操作有 读取,编辑,删除,合并,在写操作时,需要考虑资源现在是谁在用,是否释放。必须等一次操作完成后才能进行下一种操作。
设计:
结果:
{} is eating!Orc0
{} is eating!Orc1
{} is eating!Orc2
{} has finished eating!Orc0
{} has finished eating!Orc2
{} has finished eating!Orc1
{} has started to roam in the wastelands.Orc0
{} has started to roam in the wastelands.Orc2
{} has started to roam in the wastelands.Orc1
实现:
package com.study17_1_active;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 活动生物类是活动对象示例的基础。
* <p>
* 对于每个驻留在其控制线程中的对象,活动对象设计模式将方法执行与方法调用分离。目标是通过使用异步方法调用引入并发性,并使用调度器处理请求。
* <p>
* 实现活动对象模式的类将包含自同步机制,而不使用“synchronized”方法
* <p>
* 要实现一个拥有自己的控制线程机制的生物,并且只公开它的API而不公开执行本身,我们可以使用活动对象模式。
*/
public abstract class ActiveCreature {
private BlockingQueue<Runnable> requests;
private String name;
private Thread thread;
private int status;
protected ActiveCreature(String name) {
this.name = name;
this.status = 0;
this.requests = new LinkedBlockingQueue<>();
thread = new Thread(() -> {
boolean infinite = true;
while (infinite) {
try {
requests.take().run();
} catch (InterruptedException e) {
if (this.status != 0) {
System.out.println("Thread was interrupted. --> {}" + e.getMessage());
}
infinite = false;
Thread.currentThread().interrupt();
}
}
});
thread.start();
}
public void eat() throws InterruptedException {
requests.put(() -> {
System.out.println("{} is eating!" + name());
System.out.println("{} has finished eating!" + name());
});
}
public void roam() throws InterruptedException {
requests.put(() ->
System.out.println("{} has started to roam in the wastelands." + name())
);
}
public void kill(int status){
this.status = status;
this.thread.interrupt();
}
public String name() {
return this.name;
}
public int getStatus() {
return status;
}
}
package com.study17_1_active;
import java.util.ArrayList;
import java.util.List;
public class App implements Runnable {
private static final int NUM_CREATURES = 3;
public static void main(String[] args) {
var app = new App();
app.run();
}
@Override
public void run() {
List<ActiveCreature> creatures = new ArrayList<>();
try {
for (int i = 0; i < NUM_CREATURES; i++) {
creatures.add(new Orc(Orc.class.getSimpleName() + i));
creatures.get(i).eat();
creatures.get(i).roam();
}
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
Thread.currentThread().interrupt();
} finally {
for (int i = 0; i < NUM_CREATURES; i++) {
creatures.get(i).kill(0);
}
}
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.study17_1_active;
/**
* An implementation of the ActiveCreature class.
* @author Noam Greenshtain
*
*/
public class Orc extends ActiveCreature {
public Orc(String name) {
super(name);
}
}