Package com.me.config;
Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;
/**
* Project:me
* Package:com.me.config
* DATE:2017/11/22 19:30
* Author:simeon
*/
Class Demo extends Thread
{
public void Run ()
{
Jedis jedis1 = new Jedis ();
for (int i=0;i<100;i++) {
int num = Integer.parseint (jedis1.get ("num"));
num = num + 1;
Jedis1.set ("num", num+ "");
System.out.println (jedis1.get ("num"));
}
}
}
public class test{
public static void Main (String ... args) {
Jedis Jedis = new Jedis ();
Jedis.set ("num", "1");
New Demo (). Start ();
New Demo (). Start ();
}
}
One num is stored first, and the value is 1. Run 2 threads, each of which is 100 cycles +1.
Reasoning if thread safety, two threads should be mutually exclusive when running, that is, one thread is running and the other is in a blocking state, so the output should be 200.
But the result output is not 200. So Redis is not thread safe.
Redis Thread Safety discussion