Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions use of synchronozation

Have Programming Question? Ask it here!
 
 
 


kalavathi
use of synchronozation
2 Answer(s)      5 years and 8 months ago
Posted in : Java Interview Questions



Advertisement
View Answers

December 1, 2008 at 12:13 AM


Hi friend,


Synchronozation is a mechanism which is used to provide the priority for
multiple thread programs to avoid the deadlock condition


If there is a portion of code within a method or the
complete method that can be accessed by multiple threads,
that section of code is called critical section. To make
sure that only one thread can access critical section at a
time, we synchronize critical section. Synchronization is
the keyword to avoid concurrent access to critical section
of the code.

There are two ways to synchronized the execution of code:

1. Synchronized Methods

2. Synchronized Blocks (Statements)

Synchronized Methods :

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){}
}
}
}




December 1, 2008 at 12:15 AM


Synchronized Blocks (Statements)

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){}
}
}
}
}

For more information on Synchronization visit to :

http://www.roseindia.net/java/thread/synchronization.shtml

http://www.roseindia.net/java/java-methods/java-method-synchronized.shtml

http://www.roseindia.net/tutorialsearch/?t=synchronized

Thanks





Related Tutorials/Questions & Answers:
use of synchronozation - Java Interview Questions
use of synchronozation  once compare arraylist and vector,arraylist is not synchronization and not athread safe,so what are the advantages of synchronization with examples.   Hi friend, Synchronozation
the use of ForwardAction
the use of ForwardAction  What is the use of ForwardAction
Advertisements
use forms.
use forms.  How do I use forms
use of annotation.
use of annotation.  for concurrency which annotation can be used
use is of 2
use is of 2  why we use 2 in "Integer.parseInt(str,2);" for binary to decimal conversion..??   ya i got it... because binary contain only two o and 1...so here use 2...same as octal has 8
Use of jQuery
Use of jQuery  What is the use of jQuery?   Hi, jQuery is JavaScript Ajax library. Developer is using it to create rich internet applications. Read at jQuery Tutorial. Thanks
use of jquery
use of jquery  what is a jquery? what is the evalution of jquery?   Hello Friend, Please visit the following link: JQuery Tutorials Thanks
use of splitpane
use of splitpane  using JSplitPane split pane in three part. 1st in textField 2nd label and 3rd description. in textField we enter the primary key in textField than it show its all two part as label(image) and description of user
Java Use cases
Java Use cases  What are use cases
How to use Servlet and Ajax?
How to use Servlet and Ajax?  How to use Servlet and Ajax
use of object tag
use of object tag  how to use object tag in html
use of ejb 3.0
use of ejb 3.0  what is use of ejb 3.0
use of return statement
use of return statement  what is the use of return statement
What is the use of friend function?
What is the use of friend function?  What is the use of friend function
What is the use of DESC in SQL?
What is the use of DESC in SQL?  What is the use of DESC in SQL
Use of immediate attribute
Use of immediate attribute  What is the use of immediate attribute
SQL Use
SQL Use       SQL Use... The Tutorial illustrate an example from 'SQL Use'. To understand and grasp the 'SQL Use' we create a database 'komal' using create database keyword
Use of FTPClient
In this section we will discuss about use of FTPClient class with example
What is use of a abstract variable?
What is use of a abstract variable?   Hi, What is use of a abstract variable? thanks
What is use of a abstract variable?
What is use of a abstract variable?   Hi, What is use of a abstract variable? thanks
Use javax.servlet.http.HttpServletResponse object
Use javax.servlet.http.HttpServletResponse object  How do use javax.servlet.http.HttpServletResponse object to return Text data and Binary data
How to use AT Commands with J2ME?
How to use AT Commands with J2ME?  How to use AT Commands with J2ME? Thank you for your answer
use ajaxtag in Struts1.3
use ajaxtag in Struts1.3  How can use ajax tag library in jsp file in struts 1.3
use of loops and screenshots
use of loops and screenshots  1.how to use loops in netbeans? 2.how to draw a correct screenshot
use tables to structure forms
use tables to structure forms  How can I use tables to structure forms
use tables to structure forms
use tables to structure forms  How can I use tables to structure forms
Use if in velocity
Use if in velocity       This example shows you how to use if statement in velocity. Description of methods used in the example given below.... 1:- Initialize velocity run time
how to use mouse pressed
how to use mouse pressed   to action mouse clicked
how to use calender in php
how to use calender in php  how to put calender in php
What is the use of Bluetooth technology
What is the use of Bluetooth technology  What is the use of Bluetooth technology?   Bluetooth technology is used for Wireless communication between equipment's
use of ActionMapping in validate()
use of ActionMapping in validate()  why do we pass actionmapping in validate(ActionMapping mapping,HttpServletRequest) even we don't use actionmapping in that method
Use of Selectors
Uses of jQuery Selectors       Uses of jQuery Selectors In this section we are discussing the use of jQuery selectors. For using JQuery library most effectively
What is the use of Java?
What is the use of Java?  Hi, What is the use of Java programming Language? How Java is useful for creating applications? Thanks   Java.... You can check more details at What is the use of java?. Thanks
Use of CASCADE CONSTRAINTS
Use of CASCADE CONSTRAINTS  hii, What is the use of CASCADE CONSTRAINTS?   hello,, CASCADE CONSTRAINTS describes about the parent-child relationship between tables
how to use button in array
how to use button in array  plz tell me how to code for button in array
use of package concepts
use of package concepts  i m getting error when i use .* method to access all package files. when i use this through another method like... want to use packagename.* methhod. please solve the problem. i just start
Use javascript loops..
Use javascript loops..  Write a Javascript code to create a redirection script based on day of the week
how to use an editable combobox
how to use an editable combobox   Hello Everyone!!!!!!!! I have a jcombo box with certain values and i want to use it as an editable that means when user will type something in combobox, according to that text only matching item
What is the use of CASCADE CONSTRAINTS?
What is the use of CASCADE CONSTRAINTS?   What is the use of CASCADE CONSTRAINTS?   Hi, The CASCADE CONSTRAINTS clause drops all referencial integrity constraints that refer to the primary and unique keys defined
How to Use Collections
How to Use Collections  how to use ArrayList and HashMaps in case of user defined class  Hi Friend, Please visit the following links: http://www.roseindia.net/java/jdk6/ http://www.roseindia.net/javacodeexamples
how to use JOptionPane.YES_NO_OPTION
how to use JOptionPane.YES_NO_OPTION  As i said earliar i am making... to use JOptionPane.YESNOOPTION so that i can give user choice of yes or no before logging out finally.. i also thinking how to use the input (yes or no ) given
What is the use of DBCC commands?
What is the use of DBCC commands?  What is the use of DBCC commands?   Hi, DBCC stands for database consistency checker. We use these commands to check the consistency of the databases, i.e., maintenance, validation
How to use dojoscript with jsf?
How to use dojoscript with jsf?  Hi all , am new to jsf. can anyone help me out in using dojo script frame work with jsf. how to integrate dojo into jsf. Thanks in advance
how to use setAttribute
how to use setAttribute  how to get following codes using setAttributes in java??? Employee xlink:type="extended" xlink:show="new" xlink:actuate="onRequest
use of plugins - Swing AWT
use of plugins  hi dear i m making a project on java swing .i needed a plugins for search buttons........  https://swingexplorer.dev.java.net/tips.html Thanks
Unable to use JMXMP with Knopflerfish?
Unable to use JMXMP with Knopflerfish?  I have a bundle which runs properly with Eclipse, then I generate the jar file and try to use it with Knopflerfish, then I get the following exception java.net.MalformedURLException
how to use jstl tags?
how to use jstl tags?  I installed jstl jar in my libraries. I got the following error in console screen. I need the solution to resolve this uri in web.xml. please help me.......... "The absolute cannot be resolved
Use an application and applet together
Use an application and applet together  i have a project in my school in which i am making a game. i have a problem that i have an applet in that game and i want to work it in an application or to run that application
Use an application and applet together
Use an application and applet together  i have a project in my school in which i am making a game. i have a problem that i have an applet in that game and i want to work it in an application or to run that application
Use Ordered List In JSP
Use Ordered List In JSP   I created a Sting that contains ... . This string is generated dynamically from the db. When I place the string in the jsp, it does not create the ordered list. I can paste that string in the jsp
Advertisements
 

 

 

DMCA.com