• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jqplus question.

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID :952739431860
Given the following program, which one of these statements is true?
public class TestClass extends Thread
{
static Object lock1 = new Object();
static Object lock2 = new Object();
static volatile int i1, i2, j1, j2, k1, k2;
public void run()
{
while (true)
{
workWithLocks();
workWithoutLocks();
}
}
void workWithLocks()
{
synchronized(lock1) { i1++ ; i2++; }
synchronized(lock2) { k1++ ; k2++ ; }
j1++; j2++;
}
void workWithoutLocks()
{
if (i1 != i2) System.out.println("i");
if (j1 != j2) System.out.println("j");
if (k1 != k2) System.out.println("k");
}
public static void main(String args[])
{
new TestClass().start();
new TestClass().start();
}
}
Answer given is:
One cannot be certail whether any of the letters i,j and k will be printed during execution.
While I agree with this,I also feel that the other option which states that "One can be certain that the letters i and k will never be printed" is also true as i and k are incremented together.Please clarify.
Regards,
Rashmi
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi,
you have explaned me the same que.
Please see the link.... and ther is my another query if anyone can tell me what that mean??
http://www.javaranch.com/ubb/Forum24/HTML/012762.html
------------------
Regards
Ravish
 
Rashmi Hosalli
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravish,
I know I did post for a similar/same Q, but this was and is my doubt which unfortunately still remains unanswered!!!
Rashmi.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I think that the problem is workWithoutLocks() method, you can read the i1 fresh incremented by the one thread and the i2 having the old value (incremented the other thread).
The incrementing is sync. but not the readin.
..Cristian
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also interested in some additional clarification to this question. I added a little to the println statement to get an understanding of what is going on, otherwise the code is the same.
void workWithoutLocks()
{
if (i1 != i2) System.out.println("i's not equal "+ i1 + ": " + i2);
if (j1 != j2) System.out.println("j's not equal "+ j1 + ": " + j2);
if (k1 != k2) System.out.println("k's not equal "+ k1 + ": " + k2);
}

I ran this about a dozen times and each and ever time, I got a println statement. Some with only j's being different and some with all three. Some examples:
j's not equal 152675: 152674
k's not equal 362960: 362959
i's not equal 800684: 800683
Process Exit...
j's not equal 89552: 104228
j's not equal 89553: 104229
j's not equal 89554: 104230
j's not equal 89555: 104231
j's not equal 89556: 104232
j's not equal 89557: 104233
j's not equal 89558: 104234
I can understand how the i's and k's can be reported as being different. Say that thread1 just finishes the k1++ statement and is swapped out for the thread2. If thread2 is now performing the workWithoutLocks(), it will report that k1 is different than k2. Now when thread1 has the processor it will increment k2.
Hopefully, someone can clear up how j1 and j2 can be so different as in (j's not equal 89558: 104234).

I can't agree with the answer that "One cannot be certail whether any of the letters i,j and k will be printed during execution." Even if the answer was related to a time-slice vs. preemptive, thread1 can be removed just after the first increment and before the second increment in either case.

 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I posted a reply and it just dissappeared. So here goes try number two.
I also want to get some additional clearification on what is going on with this example. To get a better handle on what is happening I modified the println statement, otherwise the code is the same:
void workWithoutLocks()
{
if (i1 != i2) System.out.println("i's not equal "+ i1 + ": " + i2);
if (j1 != j2) System.out.println("j's not equal "+ j1 + ": " + j2);
if (k1 != k2) System.out.println("k's not equal "+ k1 + ": " + k2);
}
I ran the example about a dozen times and each and ever time I got a println statement. Sometimes with j's being different and sometimes with all three being different. Some examples:
j's not equal 152675: 152674
k's not equal 362960: 362959
i's not equal 800684: 800683
Process Exit...

j's not equal 89552: 104228
j's not equal 89553: 104229
j's not equal 89554: 104230
j's not equal 89555: 104231
j's not equal 89556: 104232
j's not equal 89557: 104233

I can understand how it can be reported that the i's and the k's can be different. Consider if thread1 just finishes incrementing i1 and is then swapped out for thread2 before incrementing i2. Now thread2 gets the cpu and enters the workWithoutLocks(), compares the i's and reports that they are different.
I am having a hard time accepting that the answer is "One cannot be certail whether any of the letters i,j and k will be printed during execution." I was able to produce a println result with each run. Even if the answer was directed to the difference between preemptive vs. time slice scheduling, this swapping of threads will occur and can produce the result. Hopefully Paul Anil will read this and give us a complete answer.
Can anyone explain how the j's can be so different (j's not equal 89557: 104233)?
 
Evil is afoot. But this tiny ad is just an ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic