微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用多线程访问单个 Jedis 客户端

如何解决使用多线程访问单个 Jedis 客户端

我正在尝试访问具有多个线程的 jedis 客户端,其中一个线程可以简单地读取和写入数据库,而另一个线程可以订阅和侦听频道。这在绝地武士中可能吗?或者,是否可以通过任何其他同样有效的客户端将变量写入一个客户端。

导入redis.clients.jedis.Jedis; 导入 redis.clients.jedis.JedisPubSub;

public class Testing1 {
public static void main(String args[])
{
   final Jedis jedis = new Jedis("localhost");
    Runnable runnable =()->
    {
        System.out.println(jedis.clientId());
        jedis.set("acd","acg");
        String str=jedis.get("acd");
        System.out.println(str);

    };
    Runnable runnable2 =()->
    {
        System.out.println(jedis.clientId());
        JedisPubSub jedisPubSub = new JedisPubSub()
        {
            @Override
            public void onMessage(String channel,String message) {
                System.out.println("Channel " + channel + " has sent a message : " + message );
                if(channel.equals("C1")) {
                    /* Unsubscribe from channel C1 after first message is received. */
                    unsubscribe(channel);
                }
            }

            @Override
            public void onSubscribe(String channel,int subscribedChannels) {
                System.out.println("Client is Subscribed to channel : "+ channel);
                System.out.println("Client is Subscribed to "+ subscribedChannels + " no. of channels");
            }

            @Override
            public void onUnsubscribe(String channel,int subscribedChannels) {
                System.out.println("Client is Unsubscribed from channel : "+ channel);
                System.out.println("Client is Subscribed to "+ subscribedChannels + " no. of channels");
            }

        };
        jedis.subscribe(jedisPubSub,"__redis__:invalidate");
    };


  / Thread thread= new Thread(runnable);
    Thread thread1= new Thread(runnable2);
    thread.start();
    thread1.start();
    jedis.set("harry","veer");
    System.out.println(jedis.get("harry"));
}

}

这就是我正在尝试的基本方法,请帮助我

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。