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.
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 ?
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 !!
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 ?
very good post!!! Thanks for sharing with us... It is more useful for us..
ReplyDeleteI 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
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.
ReplyDeleteSelenium Training in Electronic City
Such a great information for blogger i am a professional blogger thanks…
ReplyDeleteSoftgen Infotech is the Best Oracle Training institute located in BTM Layout, Bangalore providing quality training with Realtime Trainers and 100% Job Assistance.
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..
ReplyDeleteExtremely 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..
ReplyDeleteMicrosoft 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
Really awesome blog your blog is very useful and informative for me.
ReplyDeletehttps://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
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.
ReplyDeletepython 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
This is my first visit to your blog! We are a team of volunteers and new
ReplyDeleteinitiatives in the same niche. Blog gave us useful information to work. You
have done an amazing job!
Salesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
This comment has been removed by the author.
ReplyDeletemongodb training in chennai
ReplyDeleteoracle training in chennai
cloud computing training in chennai
aws training in chennai
Lahore Park View Villas, Park View City Islamabad , Lahore Smart City, Blue world city Islamabad. These are the leading real estate projects deal by Regency Marketing
ReplyDeleteEN SON ÇIKAN PERDE MODELLERİ
ReplyDeletesms onay
mobil ödeme bozdurma
Nft Nasıl Alinir
Ankara evden eve nakliyat
trafik sigortasi
DEDEKTÖR
KURMA WEB SİTESİ
Ask romanlari
SMM PANEL
ReplyDeleteSmm Panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
https://www.beyazesyateknikservisi.com.tr/
SERVİS
TİKTOK JETON HİLESİ İNDİR
Good content. You write beautiful things.
ReplyDeletesportsbet
vbet
vbet
hacklink
mrbahis
mrbahis
sportsbet
korsan taksi
taksi
Success Write content success. Thanks.
ReplyDeletebetpark
canlı poker siteleri
betmatik
deneme bonusu
kralbet
betturkey
canlı slot siteleri
Hey
ReplyDeleteAfter 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
maraş
ReplyDeletebursa
tokat
uşak
samsun
SBP5UM
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.
ReplyDeletesalt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
ZR2U
salt likit
ReplyDeletesalt likit
E40
FHJNGJM
ReplyDeleteشركة تركيب كاميرات مراقبة بجازان