Redis Thread Safety Discussion

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.