ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Java Related >> Java J2EE >> Core Java
 
 


 

 
 Core Java interview questions  Core Java Interview Questions (1720)
 Advanced Java interview questions  Advanced Java Interview Questions (331)
 Swing interview questions  Swing Interview Questions (43)
 EJB interview questions  EJB Interview Questions (134)
 Servlets interview questions  Servlets Interview Questions (255)
 Struts interview questions  Struts Interview Questions (195)
 JDBC interview questions  JDBC Interview Questions (187)
 JMS interview questions  JMS Interview Questions (9)
 SunOne interview questions  SunOne Interview Questions (3)
 J2EE interview questions  J2EE Interview Questions (71)
 Weblogic interview questions  Weblogic Interview Questions (145)
 Websphere interview questions  Websphere Interview Questions (14)
 Java Networking interview questions  Java Networking Interview Questions (20)
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions (87)
Question
What are the two ways you can synchronize a block of code?
 Question Submitted By :: Manoj Pandey
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What are the two ways you can synchronize a block of code?
Answer
# 1
There are two ways to syanchronize access to an object

1st one is using synchronized keyword for example

Synchronized type methodname(arg-list)
{
//synchrinized method body
}

2nd way s using synchronized block

synchronized(object)
{
//synchronized statement

}
 
Is This Answer Correct ?    28 Yes 0 No
Vikki
 
  Re: What are the two ways you can synchronize a block of code?
Answer
# 2
bu using thread in your main program 
Is This Answer Correct ?    2 Yes 16 No
Guest
 
 
 
  Re: What are the two ways you can synchronize a block of code?
Answer
# 3
The way you can synchronize the block is:-

synchronized(dummy object)
{
//synchronized statement

}
In dummy object you can use this or u can create a dummy
object by using new Keyword.

What answer Vikki is posted as the first one is not the way
of synchronizing the block but it's a way to synchronizing
a method.
 
Is This Answer Correct ?    4 Yes 0 No
Ajay Tiwary
 
  Re: What are the two ways you can synchronize a block of code?
Answer
# 4
I think using mutex is the second way.
Because synchronizing an object is useless
 
Is This Answer Correct ?    1 Yes 0 No
Popol83
 
  Re: What are the two ways you can synchronize a block of code?
Answer
# 5
There are two ways to synchronized the execution of code:
1. Synchronized Methods
2. Synchronized Blocks (Statements)

Synchronized Methods
If any method is specified with the keyword synchronized then this method of an object is only executed by one thread at a time. A any thread want to execute the synchronized method, firstly it has to obtain the objects lock. Acquire the method is simply by calling the method. If the lock is already held by another thread, then calling thread has to wait.

public class SynThread{
public static void main(String args[]){
Share s=new Share();
MyThread m1=new MyThread(s,"Thread1");
MyThread m2=new MyThread(s,"Thread2");
MyThread m3=new MyThread(s,"Thread3");
}
}
class MyThread extends Thread{
Share s;
MyThread(Share s,String str){
super(str);
this.s=s;
start();
}
public void run(){
s.doword(Thread.currentThread().getName());
}
}
class Share{
public synchronized void doword(String str){
for(int i=0;i<5;i++){
System.out.println("Started :"+str);
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
}

Synchronized Blocks (Statements)
A synchronized statement is another way to create synchronized code. Synchronized statements must specify the object that provides the intrinsic lock. The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.
General form of synchronized block is:
synchronized (object reference expression)
{
code block..
}

public class SynStatement{
public static void main(String args[]){
Share s=new Share();
MyThread m1=new MyThread(s,"Thread1");
MyThread m2=new MyThread(s,"Thread2");
MyThread m3=new MyThread(s,"Thread3");
}
}
class MyThread extends Thread{
Share s;
MyThread(Share s,String str){
super(str);
this.s=s;
start();
}
public void run(){
s.doword(Thread.currentThread().getName());
}
}
class Share{
public void doword(String str){
synchronized(this){
for(int i=0;i<5;i++){
System.out.println("Started :"+str);
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
}
}


http://www.roseindia.net/java/thread/synchronization.shtml
 
Is This Answer Correct ?    3 Yes 1 No
Supriyo Pal
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
can we have virtual functions in java? Wipro 11
steps to connect with Oracle Databse using TYPE-2 Jdbc driver.   3
I want to run a simple hello world java (HelloWorld.java) program using a batch file. How can i run it and how to construct a batch file. Infosys 1
Can multiple catch statements be used in exceptions ? Wipro 6
What is the preferred size of a component?   2
How to convert String into primitive datatype.   6
Write the code for Palindrome ? Bally-Technologies 2
can you use the two main method in same class?how? DELL 4
What is inheritance?   5
why abstract class will have a constructor?   3
what are the different access specifiers that can be used by interfaces and abstract classes? can anyone give me detailed description on this IBM 10
what are the analysis of an object   2
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com