CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: How to make application thread-safe ?

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



Category Java Interview Questions & Answers - Exam Mode / Learning Mode
Rating (1.8) By 186 users
Added on 10/13/2009
Views 11748
Rate it!
Question: How to make application thread-safe ?
Answer:

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. Source: CoolInterview.com


Using synchronized keyword you can make thread safe application and you can also use wait(), notify(), notifyall(). All these methods are of Object class. Source: CoolInterview.com

Answered by: modi | Date: 1/30/2009 | 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. Source: CoolInterview.com

Answered by: Harsha Pathak | Date: 3/23/2009 | Contact Harsha Pathak Contact Harsha Pathak

We can implement the interface as SingleThreadModel. Source: CoolInterview.com

Answered by: ravi kumar | Date: 5/8/2009 | 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..
Source: CoolInterview.com

Answered by: Swagatam Mitra | Date: 6/29/2009 | 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 Source: CoolInterview.com

Answered by: iimamuddin | Date: 7/3/2009 | 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. Source: CoolInterview.com

Answered by: Prasann Kumar Mishra | Date: 11/2/2009 | 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 Source: CoolInterview.com

Answered by: Kundan KumarBharti | Date: 11/13/2009 | 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.. Source: CoolInterview.com

Answered by: satish | Date: 12/31/2009 | 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.
Source: CoolInterview.com

Answered by: sonu solanki | Date: 3/27/2010 | 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. Source: CoolInterview.com

Answered by: Sanu | Date: 4/26/2010 | 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.

Post your answer here

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?
String str = new String("abc - Java Interview Questions & Answers" class="btn btn-primary btn-xs">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
How can you force all derived classes to implement a method present in the base class?
View Answer
What is the difference between an Applet and an Application?
View Answer
Java says "write once, run anywhere". What are some ways this isn't quite true?
View Answer
Describe java's security model.
View Answer
What is the difference between a Vector and an Array. Discuss the advantages and disadvantages of both?
View Answer
How many different types of JDBC drivers are present? Discuss them.
View Answer
What does the keyword "synchronize" mean in java. When do you use it? What are the disadvantages of synchronization?
View Answer
What are native methods? How do you use them?
View Answer
What is RMI?
View Answer
What is JDBC? Describe the steps needed to execute a SQL query using JDBC.
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Java Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.015625 Seconds