CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS J2EE JAVA DETAILS
Question :
How to make application thread-safe ?
Category Java Interview Questions
Rating (3.7) By 82 users
Added on 10/13/2004
Views 10635
Rate it!
Answers:

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.



Posted by: modi    

Contact modi Contact modi

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.



Posted by: Harsha Pathak    

Contact Harsha Pathak Contact Harsha Pathak

We can implement the interface as SingleThreadModel.



Posted by: ravi kumar    

Contact ravi kumar Contact ravi kumar

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..



Posted by: Swagatam Mitra    

Contact Swagatam Mitra Contact Swagatam Mitra

To make the application thread safe we must use keyword synchronized with the block of code i.e with method



Posted by: iimamuddin    

Contact iimamuddin Contact iimamuddin

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.



Posted by: Prasann Kumar Mishra    

Contact Prasann Kumar Mishra Contact Prasann Kumar Mishra

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



Posted by: Kundan KumarBharti    

Contact Kundan KumarBharti Contact Kundan KumarBharti

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..



Posted by: satish    

Contact satish Contact satish

There are two ways that enables us to make application which is thread safe.
1. By implementing SingleThreadModel interface.
2. By Synchronization.



Posted by: sonu solanki    

Contact sonu solanki Contact sonu solanki

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.



Posted by: Sanu    

Contact Sanu Contact Sanu

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    What is it reflection (introspection) ? Why is reflection possible in the Java language?
    View Answer
    Why are Java ARchive (JAR) files important?
    View Answer
    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?
    View Answer
    What are the main differences between Java and C++?
    View Answer
    What are interfaces?
    View Answer
    How can you achieve Multiple Inheritance in Java?
    View Answer
    What is the difference between StringBuffer and String class?
    View Answer
    Describe, in general, how java's garbage collector works?
    View Answer
    What's the difference between == and equals method?
    View Answer
    What are abstract classes, abstract methods?
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL Java Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/463/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

    0.97
    Cache = 0 Seconds