Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads And Synchronization  RSS feed

 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is meant by synchronization?
why synchronization is associated with threads?
can we use synchronized methods without threads?
these are the doubts i face and this doubt struck me when i came accross this problem given below?please help
public class A
{
public static void main(String args[])
{
new A.method();
}
public synchronized void method(){}
}//class A
class B extends A
{
<access modifier???>void method(){}
}
please tell me about the what are the access modifier by explaining about the concepts of synchronization and overriding with respect to synchronization?
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I don't know how well I can address the issue of overriding synchronized methods, but I can at least answer your questions about synchronization in general.
Syncronized methods in an object are all members of a team, if you will. These methods stick together in that only one thread can use any part of that team at a given time. Once that thread is finished executing the method, another thread can then use one of the synchronized methods.
Each Object has what is called a Lock. While none of the synchronized methods are being executed, that lock is available to anything that needs it. Once a single thread begins execution of a synchronized method, that lock is no longer available. It is being "Held" by that thread. In order to execute a syncronized method in this object, a thread must be holding that lock. This means that if another thread wishes to run one of the object's synchronized methods, it must wait in line for the lock (kind of like the Conch in "Lord of the Flies"). Once the first thread is finished the method, it releases the lock and the second thread can now hold that lock and execute the synchronized method it had been waiting for.
Here is a quick example to clear up this mumbo jumbo:
public class Printer {
public void printA(){
System.out.print("sh");
System.out.print("ow");
System.out.println(" me the money");
}
public void printB(){
System.out.print("it");
System.out.println(" is a good day to Die");
}
public static void main(String [] args){
final Printer p = new Printer();
Thread t1 = new Thread(){public void run(){p.printA();}};
t1.start();
Thread t2 = new Thread(){public void run(){p.printB();}};
t2.start();
}
}
As a quick explanation of the main method, it is just creating two Thread objects (t1 and t2) that have a run method which will call printA and printB respectively of the oject called p. The run method is called automatically when you call the start method. Because t1 and t2 are different threads, they do not have to run one after the other, rather they branch off and do their own things at virtually the same time.
With the code the way it is written, there is a chance that a rather awkward print out may result. If the first line of printA is executed by t1 and then the first line of printB is executed by t2, followed by the second line of printB and then the second line of printB, you're left with "shitow me the money is a good day to die" which sounds pretty painful to me.
If we used the synchronized modifier on printA and printB, they could no longer run at the same time, so one method would completely finish executing before the the other began. Try it out and see the difference.
Now that was a big pile to understand at once. I hope that cleared up some of your synchronized questions. Oh yea, your last question about running synchronized methods without threads.
Quickly, it is certainly okay to use synchronized methods because when you run an application, it is actually running along a single thread. Synchronized code would not be needed here, but it will do no harm.
I hope that helped.
-Paul
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Thanks for that answer, it really cleared even my doubts about synchorised methods.

But I have come up with a new doubt.
Suppose if you are creating a client program which access a file or a persistant object. In following example Class B and Class C are client programs which are trying to read and write to the same file through the Object of Class A.
My question hear is, Will synchronise keyword protect that file from read and written at same time. I think not because both clients have different objects. What do u think? Can you explain me a better way to tackle this?
Thanks very much in anticipation.
class A{
synhcronise String public readFromFile();
synhcronise public writeToFile(String x);
}
class B{
public static void main (String[] args)
{
A newObject = new A();
String x = newObject.readFromFile();
}
class C{
public static void main (String[] args)
{
A newObject = new A();
newObject.writeToFile("Sanjay");
}

 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About forum name : The name should more sound like 'Threads and synchronization between them'. So, it's not about synchronizing without threads, it more about what else but threads can we synchronize
About your last doubt : You will not be able to synchronize within different JVM's, that your example requires, as well as I suppose.
There is different way for synchronizing file access, so-called file locking. You can create a special file like '.originalfilename' and as long as that file does exist, it means somebody locked it. I didn't find a way in Java to use features like flock() or smth.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Pawel but I did understand what you said there, Can you explain please???
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you want me to explain ?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pawel ,
What i want to know is how can be lock or unlock records from the client program.
Sanjay
 
Francois Le Droff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I've been trying lately as well to synchronize java processes
[i.e. not only differentthreads] that were running under the same JVM.

Well, I succeeded to implement an easy and fast solution using tryLock()
see sun api at:
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileChannel.html


May be that's what you were looking for when Pawel mention "flock()" ?

I'm about ot test this solution using processes running on different JVMs.
I'll let you know the result.

Cheers !
Francois
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!