刚开始使用简书,排版还不熟悉,见谅。
消费者类Consumer
package t2;
import redis.clients.jedis.Jedis;
public class Consumer implements Runnable {
Jedis jedis = new Jedis("127.0.0.1");
@Override
public void run() {
// TODO Auto-generated method stub
//
for(int i=0;i<6;i++){
System.out.println(jedis.blpop(0,"key1","key2"));} //blpop函数自带阻塞与非阻塞机制,第一个数字表示的是一个指定阻塞的最大秒数的整型值。当 timeout 为 0 是表示阻塞时间无限制。
}
}
生产者类Producer
package t2;
import redis.clients.jedis.Jedis;
public class Producer implements Runnable{
static Jedis jedis = new Jedis("127.0.0.1");
public static void push(String key,String msg){
jedis.rpush(key, msg);
}
public static String getMsg(String tel,String content){
return tel+content;
}
@Override
public void run() {
// TODO Auto-generated method stub
String msg=Producer.getMsg("tel1","验证码1");
Producer.push("key1",msg );
String msg1=Producer.getMsg("tel2","广告1");
Producer.push("key2",msg1 );
String msg2=Producer.getMsg("tel3","验证码2");
Producer.push("key1",msg2 );
String msg3=Producer.getMsg("tel4","验证码3");
Producer.push("key1",msg3 );
String msg4=Producer.getMsg("tel5","广告2");
Producer.push("key2",msg4 );
String msg5=Producer.getMsg("tel6","广告3");
Producer.push("key2",msg5 );
// jedis.del("key1");
// jedis.del("key2");
}
}
调用两个线程Test类
package t2;
public class Test {
public static void main(String[] args){
Consumer d1=new Consumer();
Producer d2=new Producer();
Thread t1=new Thread(d1);
Thread t2=new Thread(d2);
t1.start();
t2.start();
}
}
新手刚学习Jedis,共同进步。
网友评论