You should use the word synchronized to mark the critical section of code. You may also use other methods of thread synchronization (see wait(), notify(), notifyAll() etc.
Using synchronized keyword you can make thread safe application and you can also use wait(), notify(), notifyall(). All these methods are of Object class.
You can also use the ReentrntLock class which implements Lock interface. It has three methods signal(), signalAll() and await() methods for synchronization. This was introduced in JDK 5.
It really not something which can be answered directly without looking at the application which you want to make thread safe. As a matter of fact to make a program thread safe all you need to do is make sure you don't have any data race ( read incosistency across threads for the shared resource ) for the resources which can be accessed by multiple threads at the same point of time. To do that these are the most important steps which needs to be followed ( I may still miss something! ) - 1) Identify the shared resources 2) make there access points ( getter and setter ) synchonised ( you don't need to make the methods synchonised which access the resources or use them , rather by just creating seperate getter/setter methods of this resources synchronised and use them instead of directly refering to the resource in other places will do the job )
Arguably there are other ways as well but this is one of the ways and can be implemented easily.
Congratulation! You just made your application thread safe..
Thread-safe means only one thread con use that code at a particular time. For making thread-safe application you have to write method with synchronized key-word or have to write that piece of code in synchronized block.
If we want to make application thread safe the we have to use synchronized keyword with method. If we use synchronized keyword then only one person can access that method . During this period method will be locked
In order to make application thread safe,the block(group of statements) or method declared as syncronized using keyword syncronize.There are two ways of syncronizng.One is Object levle Syncronization and other one is Class level Syncronization..
To make an application thread safe we can use the keyword syncronised. Also we should use class level instance variable when considering multi threaded programming.
In Java, You can create a String object as: String str = "abc"; & String str = new String("abc"); Why cant a button object be created as : Button bt = "abc" Why is it compulsory to create a button object as: Button bt = new Button("abc"); Why is this not compulsory in String's case?