Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are primitive datatypes thread-safe in Java

Are the primitive data types like int & short thread-safe in Java? I have executed the following code and couldn't see expected result 500 some times.

public class SampleThree extends Thread {     static long wakeUpTime = System.currentTimeMillis() + (1000*20);     static int inT;     public static void main(String args[])     {         System.out.println("initial:" + inT);         for(int i=0; i<500; i++)             new SampleThree().start();         try {             Thread.sleep(wakeUpTime - System.currentTimeMillis() + (1000*30));             System.out.println("o/p:" + inT);         }         catch(Exception e){             e.printStackTrace();         }     }      public void run()     {         try {             long s = wakeUpTime - System.currentTimeMillis();             System.out.println("will sleep ms: " + s);             Thread.sleep(s);             inT++; // System.out.println(inT);         }         catch(Exception e) {             e.printStackTrace();         }     } } 

Here concurrently 500 thread will update the int variable inT. Main thread after waiting for concurrent update to be completed, prints inT value.

Find similar example here

like image 556
krishna Avatar asked Feb 14 '12 14:02

krishna


People also ask

Which class is not thread-safe in Java?

When designing a class that may be used for concurrent programming—that is, a class whose instances may be used by more than one thread at a time—it is imperative that you make sure the class is " thread-safe.” Consider the IntList class of Example 2-7. This class is not thread safe.

Which is thread-safe in Java?

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.

Is Java long thread-safe?

So point is, in Java, long and double aren't thread safe. When multiple threads are going to access a long or a double value without synchronization, it can cause problems. To ensure atomic/thread safety, it is essential to use volatile to ensure changes made by one thread are visible to other threads.

Is string thread-safe in Java?

A string is immutable and therefore thread safe.


2 Answers

There are three ways in which they're not safe:

  • long and double aren't even guaranteed to be updated atomically (you could see half of a write from a different thread)
  • The memory model doesn't guarantee that you'll see the latest updates from one thread in another thread, without extra memory barriers of some kind
  • The act of incrementing a variable isn't atomic anyway

Use AtomicInteger etc for thread-safe operations.

like image 195
Jon Skeet Avatar answered Sep 22 '22 13:09

Jon Skeet


Primitive types are not thread safe. Check this tutorial.

like image 36
Parvin Gasimzade Avatar answered Sep 24 '22 13:09

Parvin Gasimzade