Only one thread t1 has access to Test.x.
Only one thread t2 has access to Test.xObj.
Monitor (implicit synchronization) of Instance class Test is not used.
There is no thread interference at all here.
Even if we remove keyword volatile and exchange optimistic locking AtomicInteger variable regular Integer the thread execution order is generally unpredictable because of not absolute contract with underlying OS in order to perform threads scheduling.
n the given code, two threads (t1 and t2) are running concurrently and printing the values of x and xObj, respectively. The execution order of the threads can vary due to their concurrent nature. Here's the breakdown:
Thread 1 (t1) is operating on the volatile int x variable. It increments x from 1 to 2 as part of the while (t.x < 3) loop.
Thread 2 (t2) is operating on the AtomicInteger xObj. It increments xObj from 1 to 2 and then to 3 as part of the while (t.xObj.get() < 3) loop.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
1 week, 1 day agoNaoufal18
3 months, 1 week agoUteman
9 months agoxplorerpj
9 months, 3 weeks agominhdev
11 months agojames2033
1 year, 1 month agoSamps
1 year, 1 month ago