使用了自己写的log4j拓展显示线程ID的jar包。
运行效果:
写道
[2014-03-27 23:36:20.818][9][com.zwh.log4jtest.Storage][INFO]: producter int <<534, and list is [].+.
[2014-03-27 23:36:21.832][9][com.zwh.log4jtest.Storage][INFO]: producter int <<92, and list is [534].+.
[2014-03-27 23:36:22.847][9][com.zwh.log4jtest.Storage][INFO]: producter int <<30, and list is [534, 92].+.
[2014-03-27 23:36:23.862][8][com.zwh.log4jtest.Storage][INFO]: consumer int is >>534, and list is [92, 30]...
[2014-03-27 23:36:24.877][8][com.zwh.log4jtest.Storage][INFO]: consumer int is >>92, and list is [30]...
[2014-03-27 23:36:25.892][10][com.zwh.log4jtest.Storage][INFO]: consumer int is >>30, and list is []...
[2014-03-27 23:36:26.907][27][com.zwh.log4jtest.Storage][INFO]: producter int <<30, and list is [].+.
[2014-03-27 23:36:27.922][25][com.zwh.log4jtest.Storage][INFO]: producter int <<783, and list is [30].+.
[2014-03-27 23:36:28.937][24][com.zwh.log4jtest.Storage][INFO]: consumer int is >>30, and list is [783]...
[2014-03-27 23:36:29.952][24][com.zwh.log4jtest.Storage][INFO]: consumer int is >>783, and list is []...
[2014-03-27 23:36:30.967][23][com.zwh.log4jtest.Storage][INFO]: producter int <<952, and list is [].+.
[2014-03-27 23:36:31.982][21][com.zwh.log4jtest.Storage][INFO]: producter int <<901, and list is [952].+.
[2014-03-27 23:36:32.997][21][com.zwh.log4jtest.Storage][INFO]: producter int <<93, and list is [952, 901].+.
[2014-03-27 23:36:34.012][20][com.zwh.log4jtest.Storage][INFO]: consumer int is >>952, and list is [901, 93]...
[2014-03-27 23:36:35.027][20][com.zwh.log4jtest.Storage][INFO]: consumer int is >>901, and list is [93]...
[2014-03-27 23:36:36.042][19][com.zwh.log4jtest.Storage][INFO]: producter int <<884, and list is [93].+.
[2014-03-27 23:36:37.057][17][com.zwh.log4jtest.Storage][INFO]: producter int <<418, and list is [93, 884].+.
[2014-03-27 23:36:38.072][17][com.zwh.log4jtest.Storage][INFO]: producter int <<323, and list is [93, 884, 418].+.
[2014-03-27 23:36:39.087][17][com.zwh.log4jtest.Storage][INFO]: producter int <<45, and list is [93, 884, 418, 323].+.
[2014-03-27 23:36:40.102][16][com.zwh.log4jtest.Storage][INFO]: consumer int is >>93, and list is [884, 418, 323, 45]...
[2014-03-27 23:36:21.832][9][com.zwh.log4jtest.Storage][INFO]: producter int <<92, and list is [534].+.
[2014-03-27 23:36:22.847][9][com.zwh.log4jtest.Storage][INFO]: producter int <<30, and list is [534, 92].+.
[2014-03-27 23:36:23.862][8][com.zwh.log4jtest.Storage][INFO]: consumer int is >>534, and list is [92, 30]...
[2014-03-27 23:36:24.877][8][com.zwh.log4jtest.Storage][INFO]: consumer int is >>92, and list is [30]...
[2014-03-27 23:36:25.892][10][com.zwh.log4jtest.Storage][INFO]: consumer int is >>30, and list is []...
[2014-03-27 23:36:26.907][27][com.zwh.log4jtest.Storage][INFO]: producter int <<30, and list is [].+.
[2014-03-27 23:36:27.922][25][com.zwh.log4jtest.Storage][INFO]: producter int <<783, and list is [30].+.
[2014-03-27 23:36:28.937][24][com.zwh.log4jtest.Storage][INFO]: consumer int is >>30, and list is [783]...
[2014-03-27 23:36:29.952][24][com.zwh.log4jtest.Storage][INFO]: consumer int is >>783, and list is []...
[2014-03-27 23:36:30.967][23][com.zwh.log4jtest.Storage][INFO]: producter int <<952, and list is [].+.
[2014-03-27 23:36:31.982][21][com.zwh.log4jtest.Storage][INFO]: producter int <<901, and list is [952].+.
[2014-03-27 23:36:32.997][21][com.zwh.log4jtest.Storage][INFO]: producter int <<93, and list is [952, 901].+.
[2014-03-27 23:36:34.012][20][com.zwh.log4jtest.Storage][INFO]: consumer int is >>952, and list is [901, 93]...
[2014-03-27 23:36:35.027][20][com.zwh.log4jtest.Storage][INFO]: consumer int is >>901, and list is [93]...
[2014-03-27 23:36:36.042][19][com.zwh.log4jtest.Storage][INFO]: producter int <<884, and list is [93].+.
[2014-03-27 23:36:37.057][17][com.zwh.log4jtest.Storage][INFO]: producter int <<418, and list is [93, 884].+.
[2014-03-27 23:36:38.072][17][com.zwh.log4jtest.Storage][INFO]: producter int <<323, and list is [93, 884, 418].+.
[2014-03-27 23:36:39.087][17][com.zwh.log4jtest.Storage][INFO]: producter int <<45, and list is [93, 884, 418, 323].+.
[2014-03-27 23:36:40.102][16][com.zwh.log4jtest.Storage][INFO]: consumer int is >>93, and list is [884, 418, 323, 45]...
代码:
package com.zwh.log4jtest; import java.util.LinkedList; import java.util.Random; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; public class Storage { Logger logger = LogManager.getLogger(Storage.class); LinkedList<Integer> storage = new LinkedList<Integer>(); Random random = new Random(); public synchronized void consume() { if(storage.size() <= 0) { try { wait(); } catch (InterruptedException e) { logger.error(e.getMessage()); } } else { Integer i = storage.remove(); logger.info("consumer int is >>" + i + ", and list is " + storage + "..."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } notify(); } } public synchronized void product() { if(storage.size() >= 10) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { Integer i = random.nextInt(1000); logger.info("producter int <<" + i + ", and list is " + storage + ".+."); storage.add(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } notify(); } } public static void main(String[] args) { Storage storage = new Storage(); for(int i = 0;i < 10;i ++) { Consumer consumer = new Consumer(storage); Producter producter = new Producter(storage); consumer.start(); producter.start(); } } } class Consumer extends Thread{ private Storage storage; public Consumer(Storage storage) { this.storage = storage; } @Override public void run() { while(true) { storage.consume(); } } } class Producter extends Thread{ private Storage storage; public Producter(Storage storage) { this.storage = storage; } @Override public void run() { while(true) { storage.product(); } } }
相关推荐
有一个线程滚动浏览数据库条目以保持合理的内存消耗和处理每个条目的多个消费者线程。 随意使用它。 如何使用 打开你最喜欢的 IDE 并导入这个项目 通过和配置数据库连接 通过配置程序 将调整为您的数据库架构 定义...
由于用户对于密码的使用非常随意(如将密码写下来并成为网络钓鱼诈骗攻击的牺牲品),因此您必须强化身份验证过程。...由于许多雇主以及面向消费者的界面(如在线银行)都采用 OTP,因此您应亲自熟悉一下此技术。
2. 消费者权益保护:描述中的"消费者协会投诉电话"提示我们关注消费者权益保护。在中国,消费者协会的投诉电话是12315,这是消费者维护自身权益的重要渠道。 3. 职业平等观念:标签为"试卷"的文件可能涉及多方面的...
1. 消费者权益保护:题目中提到了消费者协会投诉电话12315,这是中国消费者权益保护热线,当消费者在购物过程中遇到问题时,可以通过拨打这个电话进行咨询和投诉,维护自己的合法权益。 2. 合理消费:不合理要求指...
(1) 生产者通常是绿色植物,如农作物;消费者包括食草动物(如鼠)、食肉动物(如食虫鸟、蛇)等;分解者是微生物和真菌。 (2) 人是消费者。 (3) 食物链示例:农作物 → 鼠 → 食虫鸟 → 蛇。 (4) 农药积累最多...
用户可以通过访问特定URL来发送(`http://127.0.0.1:8088/JmsDemoTLQRA/MessageSend`)和接收(`http://127.0.0.1:8088/JmsDemoTLQRA/MessageReceive`)消息,这两个URL分别对应于JMS的生产者(Producer)和消费者...
基于最终重新创建 Reddit 的目标,其想法是使用不同的技术将主要功能分解为单独的提供者和消费者。 这里的一切都在进行中,并非用于生产。测试一旦设置了各个项目,您就可以通过运行./test.sh来运行测试。启动应用...
《食品委托加工合同》是企业间进行食品加工合作的重要法律文件,它规定了委托方与加工方之间的权利、义务和责任。...同时,双方都应遵守国家有关食品安全的法律法规,确保产品质量安全,保护消费者的权益。