Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt in Threads

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this from master exam by K & B.

In the folllowing code:

public cass SyncTest {
private static Foo foo=new Foo();
static class Increaser extends Thread {
public void run() {
foo.increase(2);
}
}
public static void main(String args[]) {
new Increaser().start();
new Increaser().start();
new Increaser().start();

}
}

class Foo {
private int data=23;
public void increase(int amt) {
data =data+amt;
System.out.println("Data is: " +data);

}
}
Assuming that data must be protected from corruption, what can you add to the preceding code to ensure the integrity of data ?

A. Synchrnize the run method.
B. Wrap the synchronize(this) around the call to f.increase();
C. The existing code will not compile.
D. The existing code will cause a runtime exception.
E. put in a wait call prior to invoking the increase method.
F. Sysnchronize the increase method.


Answer is F.

Here why option A and B are wrong? They also seems to be correct to me.

Thanks,
Geeta Vemula.
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three objects of Increaser in this program. So if you synchronize run method or use synchronized(this) in it, then it will not work, as all the three the run methods will run freely without interfering with each other. They will also use the same Foo instance, so the three run methods will be able to concurrently call the increase method...
[ December 07, 2008: Message edited by: Ankit Garg ]
 
Ranch Hand
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add if there would have been an option :
Wrap the synchronize(this) around the body of the method increase
This could also ensure the integrity of data as all threads are using the same instance of class Foo in the run method

Correct me if i am wrong
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for a little light :
If there was an option to synchronize on static final foo object in the
run method.
like:
static class Increaser extends Thread {
public void run() {
synchronize(foo){
foo.increase(2);
}
}
Here all three threads will be sharing same foo instance. then it would be maintaining the integrety of the program.

or there could be an option to synchronize the increase method.

class Foo {
private int data=23;
public synchronized void increase(int amt) {
data =data+amt;
System.out.println("Data is: " +data);
}

then this will also work.
[ December 07, 2008: Message edited by: Punit Singh ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Has anyone tried compiling this? I typed it in, don't think I've made any errors, but get an error on
the private static Foo foo = new Foo(); line "non-static variable cannot be accessed from a static
context". Sometimes I despair with this language.


run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
at thread4.Main.<clinit>(Main.java:18)
Could not find the main class: thread4.Main. Program will exit.
Exception in thread "main"
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/UseCodeTags
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you read K&B already, I wanted to ask you, "why do you need synchronization?"

The question asks, "how do you prevent corrupting the data"
So where is your "data" being altered so that it could be corrupted?
Put synchronization around that part.. here data is manipulated in increase method.. synchronize it..
 
    Bookmark Topic Watch Topic
  • New Topic