<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
網上都說jedis範例是非執行緒安全的,常常通過JedisPool連線池去管理範例,在多執行緒情況下讓每個執行緒有自己獨立的jedis範例,但都沒有具體說明為啥jedis範例時非執行緒安全的,下面詳細看一下非執行緒安全主要從哪個角度來看。
由上述類圖可知,Jedis類中有RedisInputStream和RedisOutputStream兩個屬性,而傳送命令和獲取返回值都是使用這兩個成員變數,顯然,這很容易引發多執行緒問題。測試程式碼如
public class BadConcurrentJedisTest { private static final ExecutorService pool = Executors.newFixedThreadPool(20); private static final Jedis jedis = new Jedis("192.168.58.99", 6379); public static void main(String[] args) { for(int i=0;i<20;i++){ pool.execute(new RedisSet()); } } static class RedisSet implements Runnable{ @Override public void run() { while(true){ jedis.set("hello", "world"); } } }
報錯:
Exception in thread "pool-1-thread-4" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Socket Closed
at redis.clients.jedis.Connection.connect(Connection.java:164)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97)
at redis.clients.jedis.Client.set(Client.java:32)
at redis.clients.jedis.Jedis.set(Jedis.java:68)
at threadsafe.BadConcurrentJedisTest$RedisSet.run(BadConcurrentJedisTest.java:26)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Socket Closed
at java.net.AbstractPlainSocketImpl.setOption(AbstractPlainSocketImpl.java:212)
at java.net.Socket.setKeepAlive(Socket.java:1310)
at redis.clients.jedis.Connection.connect(Connection.java:149)
... 9 more
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Socket Closed
at redis.clients.jedis.Connection.connect(Connection.java:164)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97)
at redis.clients.jedis.Client.set(Client.java:32)
at redis.clients.jedis.Jedis.set(Jedis.java:68)
at threadsafe.BadConcurrentJedisTest$RedisSet.run(BadConcurrentJedisTest.java:26)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
.......
主要錯誤:
ava.net.SocketException: Socket closed
java.net.SocketException: Socket is not connected
為什麼會出現這2個錯誤呢? 我們可以很容易的通過堆疊資訊定位到redis.clients.jedis.Connection的connect方法
public void connect() { if (!isConnected()) { try { socket = new Socket(); // ->@wjw_add socket.setReuseAddress(true); socket.setKeepAlive(true); // Will monitor the TCP connection is // valid socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to // ensure timely delivery of data socket.setSoLinger(true, 0); // Control calls close () method, // the underlying socket is closed // immediately // <-@wjw_add socket.connect(new InetSocketAddress(host, port), connectionTimeout); socket.setSoTimeout(soTimeout); if (ssl) { if (null == sslSocketFactory) { sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault(); } socket = (SSLSocket) sslSocketFactory.createSocket(socket, host, port, true); if (null != sslParameters) { ((SSLSocket) socket).setSSLParameters(sslParameters); } if ((null != hostnameVerifier) && (!hostnameVerifier.verify(host, ((SSLSocket) socket).getSession()))) { String message = String.format( "The connection to '%s' failed ssl/tls hostname verification.", host); throw new JedisConnectionException(message); } } outputStream = new RedisOutputStream(socket.getOutputStream()); inputStream = new RedisInputStream(socket.getInputStream()); } catch (IOException ex) { broken = true; throw new JedisConnectionException(ex); } } }
jedis在執行每一個命令之前都會先執行connect方法,socket是一個共用變數,在多執行緒的情況下可能存在:執行緒1執行到了
outputStream = new RedisOutputStream(socket.getOutputStream()); inputStream = new RedisInputStream(socket.getInputStream());
執行緒2執行到了:
socket = new Socket(); //執行緒2 socket.connect(new InetSocketAddress(host, port), connectionTimeout);
因為執行緒2重新初始化了socket但是還沒有執行connect,所以執行緒1執行socket.getOutputStream()或者socket.getInputStream()就會丟擲java.net.SocketException: Socket is not connected。java.net.SocketException: Socket closed是因為socket異常導致共用變數socket關閉了引起的。
上面是因為多個執行緒共用jedis引起的socket異常。除了socket連線引起的異常之外,還有共用資料流引起的異常。下面就看一下,因為共用jedis範例引起的共用資料流錯誤問題。
為了避免多執行緒連線的時候引起的錯誤,我們在初始化的時候就先執行一下connect操作:
public class BadConcurrentJedisTest1 { private static final ExecutorService pool = Executors.newCachedThreadPool(); private static final Jedis jedis = new Jedis("192.168.58.99", 6379); static{ jedis.connect(); } public static void main(String[] args) { for(int i=0;i<20;i++){ pool.execute(new RedisTest()); } } static class RedisTest implements Runnable{ @Override public void run() { while(true){ jedis.set("hello", "world"); } } } }
報錯:(每次報的錯可能不完全一樣)
Exception in thread "pool-1-thread-7" Exception in thread "pool-1-thread-1" redis.clients.jedis.exceptions.JedisDataException: ERR Protocol error: invalid multibulk length
at redis.clients.jedis.Protocol.processError(Protocol.java:123)
at redis.clients.jedis.Protocol.process(Protocol.java:157)
at redis.clients.jedis.Protocol.read(Protocol.java:211)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196)
at redis.clients.jedis.Jedis.set(Jedis.java:69)
at threadsafe.BadConcurrentJedisTest1$RedisTest.run(BadConcurrentJedisTest1.java:30)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "pool-1-thread-4" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
at redis.clients.jedis.Connection.flush(Connection.java:291)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:194)
at redis.clients.jedis.Jedis.set(Jedis.java:69)
at threadsafe.BadConcurrentJedisTest1$RedisTest.run(BadConcurrentJedisTest1.java:30)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Broken pipe (Write failed)
Protocol error: invalid multibulk lengt是因為多執行緒通過RedisInputStream和RedisOutputStream讀寫緩衝區的時候引起的問題造成的資料問題不滿足RESP協定引起的。舉個簡單的例子,例如多個執行緒執行命令,執行緒1執行 set hello world命令。本來應該傳送:
*3rn$3rnSETrn$5rnhellorn$5rnworldrn
但是執行緒執行寫到
*3rn$3rnSETrn$5rnhellorn
然後被掛起了,執行緒2執行了寫操作寫入了' ',然後執行緒1繼續執行,最後傳送到redis伺服器端的資料可能就是:
*3rn$3rnSETrn$5rnhellorn' '$5rnworldr
n
至於java.net.SocketException: Connection reset或ReadTimeout錯誤,是因為redis伺服器接受到錯誤的命令,執行了socket.close這樣的操作,關閉了連線。伺服器會返回復位標誌"RST",但是使用者端還在繼續執行讀寫資料操作。
jedis本身不是多執行緒安全的,這並不是jedis的bug,而是jedis的設計與redis本身就是單執行緒相關,jedis範例抽象的是傳送命令相關,一個jedis範例使用一個執行緒與使用100個執行緒去傳送命令沒有本質上的區別,所以沒必要設定為執行緒安全的。但是如果需要用多執行緒方式存取redis伺服器怎麼做呢?那就使用多個jedis範例,每個執行緒對應一個jedis範例,而不是一個jedis範例多個執行緒共用。一個jedis關聯一個Client,相當於一個使用者端,Client繼承了Connection,Connection維護了Socket連線,對於Socket這種昂貴的連線,一般都會做池化,jedis提供了JedisPool。
import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class JedisPoolTest { private static final ExecutorService pool = Executors.newCachedThreadPool(); private static final CountDownLatch latch = new CountDownLatch(20); private static final JedisPool jPool = new JedisPool("192.168.58.99", 6379); public static void main(String[] args) { long start = System.currentTimeMillis(); for(int i=0;i<20;i++){ pool.execute(new RedisTest()); } try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis() - start); pool.shutdownNow(); } static class RedisTest implements Runnable{ @Override public void run() { Jedis jedis = jPool.getResource(); int i = 1000; try{ while(i-->0){ jedis.set("hello", "world"); } }finally{ jedis.close(); latch.countDown(); } } } }
到此這篇關於使用Jedis面臨的非執行緒安全問題詳解的文章就介紹到這了,更多相關Jedis非執行緒安全問題內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45