• 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

question about thread at J@Whiz1.4

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow is the question:
Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code?
class MyClass
{
static private int myCount = 0;
int yourNumber;
private static synchronized int nextCount()
{
return ++myCount;
}

public void getYourNumber()
{
yourNumber = nextCount();
}
}
C) Each thread will get a unique number.
D) The uniqueness of the number among different Threads can't be guaranteed.
the correct answer is C),but I think it should be D),because if one thread want to get the mycount,it must initial an instanse of class MyClass,eg,mc,then mc.yourNumber.but at that time other thread may modified the value of yourNumber not use the method getYourNumber.so...,
please help me!thanx.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think c) is the right answer because:
1)The integer myCount is static
This means that no matter how many instances of MyClass created, there is only ONE myCount, which belong to MyClass but NOT any of MyClass instance.
2)The method nextCount(), which increments myCount is synchronized and static.
Since the method is static, that means no matter how you call the method, such as MyClass.nextCount or new MyClass().nextCount. It is calling the method belong to the CLASS MyClass, not belong to any of its instance.
and the method is synchronized, that means the method and the variable in it will be locked when there is one thread accessing it. Other threads calling this method will only be notified when the method is done and available again. So, the integer myCount retrieved by any thread in that program should be unique.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the static variable is declared private, so other classes can NOT modify it... this is the key problem in your understanding to it...
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Ji's point is that some other class in the same package could modify the instance variable yourNumber without using the badly named method getYourNumber(). This looks like a very poorly designed mock exam question.
Happy trails
...full of sound and fury, signifying nothing --- William Shakespeare
 
dragon ji
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx guys.
Yeah,Ken ,I think that a thread want get myCount ,it must the same package class,because the method getYourNumber return void and variable yourNumber's modifier is default.so ,I think the answer should be option D.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic