PCCommon:
public class PCCommon {
private BlockingQueue<User> queue = new LinkedBlockingQueue<User>();
//生产
public void productData(User user){
queue.add(user);
}
//消费
public void consumerData(){
User user = queue.poll();
if(user != null){
System.out.println("userid:"+user.getUserid());
System.out.println("username:"+user.getUsername());
}
}
}
ConsumerThread:
public class ConsumerThread extends Thread{
private PCCommon pcService;
public ConsumerThread(PCCommon pcService){
this.pcService=pcService;
}
@Override
public void run(){
while(true){
pcService.consumerData();
}
}
}
PCTest:
public class PCTest {
public static void main(String[] args){
PCCommon pc = new PCCommon();
ConsumerThread ct1 = new ConsumerThread(pc);
ConsumerThread ct2 = new ConsumerThread(pc);
ConsumerThread ct3 = new ConsumerThread(pc);
ct1.start();
ct2.start();
ct3.start();
User user1 = new User();
user1.setUserid(1);
user1.setUsername("username1");
pc.productData(user1);
User user2 = new User();
user2.setUserid(2);
user2.setUsername("username2");
pc.productData(user2);
User user3 = new User();
user3.setUserid(3);
user3.setUsername("username3");
pc.productData(user3);
}
}