September 29, 2014

Are primitive datatypes in java thread safe ?

In Java, we often use primitive datatypes like short, int, long, float, double, boolean etc. Are these java primitive data types atomic, reliable, threadsafe in multi threaded programming ?

This blog post tries to examine the thread safety of such primitive data types.

Let us first start with a simple testing on primitive data type 'integer'.

I've created 200 threads and set them to attack on 'integer' variable 'anInteger' to test it's stability to hold the value on concurrent thread manipulations.


public class Test extends Thread
{ static int anInteger = 0; // initialize static final long START_TIME = System.currentTimeMillis() + (1000 * 5); // 20 seconds from current time. public static void main(String[] args) throws Exception { // employ 200 threads. for(int i=0; i<200; i++) { new Test().start(); } // wait for some time so all 200 threads completes its execution. Thread.sleep(1000*20); System.out.println(anInteger); // We EXPECT TO PRINT 200. but it prints some corrupted integer value !! } public void run() { try { Thread.sleep(START_TIME - System.currentTimeMillis()); } catch(InterruptedException e) { e.printStackTrace(); } anInteger += 1; } }

The expected output 200 is not printed !!
This proves ourself that primitive datatypes are not threadsafe in java !!

Solution:

Use concurrent package classes.
AtomicInteger


Similar questions,
Are primitive types in java thread safe ?
Is primitive data types in java designed to be atomic in concurrent access paradigm ?

20 comments :

  1. very good post!!! Thanks for sharing with us... It is more useful for us..
    I am amazed by the way you have explained things in this post. This post is quite interesting and i am looking forward to read more of your posts.
    redmi service center near me
    redmi mobile service centre in chennai
    redmi note service center in chennai
    redmi service center in velachery
    redmi service center in t nagar

    ReplyDelete
  2. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Selenium Training in Electronic City

    ReplyDelete
  3. Such a great information for blogger i am a professional blogger thanks…

    Softgen Infotech is the Best Oracle Training institute located in BTM Layout, Bangalore providing quality training with Realtime Trainers and 100% Job Assistance.

    ReplyDelete
  4. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly on microsoft azure tutorial , but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..

    ReplyDelete
  5. Extremely helpful post. This is my first time i visit here. I discovered such a large number of intriguing stuff in your blog particularly its exchange. Truly its extraordinary article. Keep it up.make happy to read this..
    Microsoft Windows Azure Training | Online Course | Certification in chennai | Microsoft Windows Azure Training | Online Course | Certification in bangalore | Microsoft Windows Azure Training | Online Course | Certification in hyderabad | Microsoft Windows Azure Training | Online Course | Certification in pune


    ReplyDelete
  6. Really awesome blog your blog is very useful and informative for me.
    https://www.acte.in/reviews-complaints-testimonials
    https://www.acte.in/velachery-reviews
    https://www.acte.in/tambaram-reviews
    https://www.acte.in/anna-nagar-reviews
    https://www.acte.in/porur-reviews
    https://www.acte.in/omr-reviews
    https://www.acte.in/blog/acte-student-reviews

    ReplyDelete
  7. Really awesome blog!!! I finally found great post here.I really enjoyed reading this article. Nice article on data science . Thanks for sharing your innovative ideas to our vision. your writing style is simply awesome with useful information. Very informative, Excellent work! I will get back here.
    python training in bangalore

    python training in hyderabad

    python online training

    python training

    python flask training

    python flask online training

    python training in coimbatore
    python training in chennai

    python course in chennai

    python online training in chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hey

    After reading your post, I like it the way you describe it. It is so excellent and if someone trying to find the services providor of party bus atlanta ga  then You should check out us atleast once.

    Thanks for sharing

    ReplyDelete
  10. The explanation of primitive datatypes in Java thread programming is exceptional. The App Download It clarifies the crucial differences and usage scenarios of each datatype, ensuring developers make informed choices in their multithreading implementations.

    ReplyDelete