網上都說jedis實例是非線程安全的,常常通過JedisPool連接池去管理實例,在多線程情況下讓每個線程有自己獨立的jedis實例,但都沒有具體說明為啥jedis實例時非線程安全的,下面詳細看一下非線程安全主要從哪個角度來看。
1. jedis類圖

2. 為什么jedis不是線程安全的?
由上述類圖可知,Jedis類中有RedisInputStream和RedisOutputStream兩個屬性,而發送命令和獲取返回值都是使用這兩個成員變量,顯然,這很容易引發多線程問題。測試代碼如
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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"
);
}
}
}
|
報錯:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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
)
.......
|
主要錯誤:
1
2
|
ava.net.SocketException: Socket closed
java.net.SocketException: Socket is not connected
|
2.1 共享socket引起的異常
為什么會出現這2個錯誤呢? 我們可以很容易的通過堆棧信息定位到redis.clients.jedis.Connection的connect方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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執行到了
1
2
|
outputStream =
new
RedisOutputStream(socket.getOutputStream());
inputStream =
new
RedisInputStream(socket.getInputStream());
|
線程2執行到了:
1
2
3
|
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關閉了引起的。
2.2 共享數據流引起的異常
上面是因為多個線程共享jedis引起的socket異常。除了socket連接引起的異常之外,還有共享數據流引起的異常。下面就看一下,因為共享jedis實例引起的共享數據流錯誤問題。
為了避免多線程連接的時候引起的錯誤,我們在初始化的時候就先執行一下connect操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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"
);
}
}
}
}
|
報錯:(每次報的錯可能不完全一樣)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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)
|
送命令相關,一個jedis實例使用一個線程與使用100個線程去發送命令沒有本質上的區別,所以沒必要設置為線程安全的。但是如果需要用多線程方式訪問redis服務器怎么做呢?那就使用多個jedis實例,每個線程對應一個jedis實例,而不是一個jedis實例多個線程共享。一個jedis關聯一個Client,相當於一個客戶端,Client繼承了Connection,Connection維護了Socket連接,對於Socket這種昂貴的連接,一般都會做池化,jedis提供了JedisPool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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();
}
}
}
}
|