`
seetoit
  • 浏览: 3197 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

java生产者消费者snippet

阅读更多

public class TestFactory {

public static void main(String[] args) {

List<Integer> storage = new ArrayList<>();

BaseRole p1 = new Provider("p1", storage);

BaseRole p2 = new Provider("p2", storage);

BaseRole p3 = new Provider("p3", storage);

BaseRole c1 = new Consumer("c1", storage);

BaseRole c2 = new Consumer("c2", storage);

BaseRole c3 = new Consumer("c3", storage);

 

p1.start();

c1.start();

p2.start();

c2.start();

//p3.start();

c3.start();

}

}

 

class BaseRole extends Thread {

private List<Integer> storage;

protected int count = 0;

 

public BaseRole(String name, List<Integer> storage) {

super(name);

this.setStorage(storage);

}

 

protected void say(String msg) {

System.out.println(this.getName() + ": " + msg);

System.out.println("库存状况: " + this.storage + " = " + this.storage.size());

}

 

protected void business() throws Exception {

 

}

 

public List<Integer> getStorage() {

return storage;

}

 

public void setStorage(List<Integer> storage) {

this.storage = storage;

}

 

@Override

public void run() {

while (true) {

try {

synchronized (this.getStorage()) {

business();

// 打印输出太快,每执行一次操作都停一下

Thread.sleep(1000);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

 

class Provider extends BaseRole {

 

private AtomicInteger seq = new AtomicInteger();

 

public Provider(String name, List<Integer> storage) {

super(name, storage);

}

 

protected void business() throws Exception {

int num = seq.incrementAndGet();

if (this.getStorage().size() >= 10) {

say("===============> 库存足够,不再生产");

// 不能用sleep,因为sleep不会释放锁

this.getStorage().wait(1000);

} else {

this.getStorage().add(num);

say("生产了..." + num + "产品");

this.getStorage().notifyAll();

}

}

 

}

 

class Consumer extends BaseRole {

 

public Consumer(String name, List<Integer> storage) {

super(name, storage);

}

 

protected void business() throws Exception {

if (this.getStorage().size() <= 0) {

say("================> 库存为0,等待");

this.getStorage().wait(2000);

} else {

Integer remove = this.getStorage().remove(0);

say("消费了: " + remove + "产品,共消费" + (++this.count));

this.getStorage().notifyAll();

}

}

 

}

 

分享到:
评论

相关推荐

    SnippetEditor

    1. **J#**:作为Visual J++的后继者,J#在.NET Framework环境下提供Java语言的支持。使用SnippetEditor,J#开发者也能享受代码片段带来的便利。 2. **VB.NET**:Visual Basic .NET的缩写,是.NET Framework的一部分...

    C# Snippet.zip

    《C#代码片段:提升开发效率的神器》 在编程世界中,效率是开发者的生命线。C#作为一款强大的编程语言,其丰富的特性和库为开发者提供...所以,无论是初学者还是经验丰富的开发者,这个压缩包都是一个值得拥有的工具。

    Snippet Editor 代码段编辑工具

    1. **代码片段创建**:用户可以自定义代码片段,保存各种编程语言中的常用函数、模板或结构,支持包括但不限于C++, Java, Python, JavaScript等多种语言。 2. **分类管理**:为了方便查找,代码片段可以根据不同的...

    Snippet Editor 代码段修改工具2.0

    "Snippet Editor 代码段修改工具2.0"是一款专为Visual Studio用户设计的实用工具,支持VS2005、VS2008以及VS2010版本。这款工具的核心功能是帮助开发者管理和编辑代码片段,提升编程效率。代码片段(Code Snippets)...

    C#.NET自定义的几个Snippet

    5. **提供者(Provider)**:`prov.snippet`可能与实现某种服务提供者的代码有关。在.NET中,提供者模式是一种设计模式,用于将不同后端服务(如数据库或缓存)的实现抽象化。片段可能包含初始化提供者、调用服务...

    visual studio code snippet editor

    1. **提高生产力**:通过预设常用代码结构,减少手动输入的时间,尤其是在处理重复性工作时。 2. **一致性**:确保团队成员遵循统一的编码风格和最佳实践。 3. **学习与记忆**:对于初学者,代码片段可以帮助理解和...

    Snippet Editor

    SnippetEditor,支持C#与其它语言的代码段包括J#\VB.NET\xml\html等的创建、编辑修改等,不知道为什么, vs2008 默认没有带代码段编辑器,一个非常好的编辑器,可以对Visual2005和visual2008 代码段进行编辑的工具,...

    高级java笔试题-snippet:各种语言的代码片段和一些文档

    高级java笔试题 Snippets The code snippets of all kinds of languages and some docs. () 、、、、、、、、、、、、、、、、 其它一些网站 前端开发 DOCS RESTfull API 微服务(Micro-Service) | | 点击 Other | | ...

    支持windows live writer的CodeSnippet

    支持windows live writer的CodeSnippet, 对博客文章可进行代码块,代码高亮设置。支持简体中文

    snippet辅助设计

    善用VS中的Code Snippet来提高开发效率

    Snippet Compiler Live 2008 Ultimate Edition for Developers (Alpha)

    "Snippet Compiler Live 2008 Ultimate Edition for Developers (Alpha)" 是一款专为开发者设计的C# 3.0代码片段编译器。...配合.NET Framework的相应版本,它可以作为一个强大的辅助工具,提升开发者的生产力。

    Snippet Compiler

    Snippet Compiler 是一个基于 Windows 的小型应用程序,你可以通过它来编写、编译和运行代码。如果你具有较小的代码段,并且你不想创建完整的 Visual Studio .NET 项目(以及该项目附带的所有文件),则该工具会很...

    Snippet Compiler 3.0.2 老朽痴拙汉化版(代码段编译器)

    Snippet Compiler3.0.2 老朽痴拙汉化版是一个免费的 .NET 编译和开发工具基于Windows 的小型应用程序,你可以通过它来编写、编译和运行代码。在下载Snippet Compiler后,无需进行安装,直接运行可执行文件即可。在第...

    SnippetEditor 2.1

    "SnippetEditor 2.1" 是一款专为Visual Studio 2008设计的代码片段编辑工具,旨在弥补原生IDE中未内置代码段编辑功能的不足。代码片段在编程中扮演着重要的角色,它们是预定义的代码块,可以方便开发者快速插入常用...

    SnippetEditor 2.1 +源码

    可以对Visual2005和visual2008 代码段进行编辑的工具,支持C++,VB,C#的代码段编辑,内含工具和源码, 官方地址:http://snippy.codeplex.com/ 以上资料皆收集于网络,仅供学习参考!~

    Python库 | python-snippet-0.1.6.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:python-snippet-0.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Snippet美化代码语法高亮工具

    Snippet主要支持JavaScript语言,但通常也会兼容其他编程语言,如HTML、CSS、Python、Java等。它通过识别代码中的关键字、注释和变量,采用不同的颜色和样式进行展示,使得代码结构清晰,色彩鲜明。这对于多人协作...

    C# to Java 代码转换工具

    Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h ...

Global site tag (gtag.js) - Google Analytics