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



Home Answers Viewqa Java-Beginners Java synchronized method
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Mangaleshwaran.M
Java synchronized method
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


Hi,

I have written below program for synchronized method in java but it is not working please help me.

program

class SynchronizedModel extends Thread
{
static String msg[]={"Welcome","mangal","to","java"};

String name;

    SynchronizedModel(String nam)
    {
    name=name;
    }
public void run()
{
display();
}

public synchronized void display()
{


try
{
    for(int i=0;i<=3;i++)
    {
    System.out.println(this.name+"  :  "+msg[i]);

    this.sleep(1000);
    }

}

catch(Exception g)
    {
    g.printStackTrace();
    }


}


public static void main(String args[])
{

SynchronizedModel s=new SynchronizedModel("Thread 1");
s.start();

SynchronizedModel s1=new SynchronizedModel("Thread 2");
s1.start();
}
}

Advertisement
View Answers

May 31, 2012 at 4:21 PM


The synchronized is a keyword used in Java ensures that only one Java thread execute an object's synchronized method at a time. The concept lies on the thread, that allows the threads to wait for resources to become available and also notify the thread that makes resource available to notify other threads are on the queues for the resources. We have modified your code. It now displays the thread name with the message you have given twice (for thread 1 and Thread 2).

class SynchronizedModel extends Thread
{
  static String msg[]={"Welcome","mangal","to","java"};
  String name;
    SynchronizedModel(String nam){
    name=nam;
    }
    public void run(){
        display();
    }

    public synchronized void display(){
    try{
    for(int i=0;i<msg.length;i++){
    System.out.println(this.name+"  :  "+msg[i]);
    this.sleep(1000);
     }
    }
    catch(Exception g){
    g.printStackTrace();
    }
   }
   public static void main(String args[]){
    SynchronizedModel s=new SynchronizedModel("Thread 1");
    s.start();

    SynchronizedModel s1=new SynchronizedModel("Thread 2");
    s1.start();
    }
   }


Related Tutorials/Questions & Answers:
Java synchronized method
Java synchronized method  What are synchronized methods and synchronized statements
Java Method Synchronized
Java Method Synchronized       The Java language Program supports multi threads. The synchronized... that help you in understanding Java Method Synchronized. We have a class Synchronized
Advertisements
Synchronized - Java Interview Questions
Synchronized  i want synchronized programs ? in java plz help me?any site   Hi Friend, If you want to know about the synchronized method.../java-method-synchronized.shtml Hope that it will be helpful for you. Thanks
Synchronized with example - Java Beginners
Synchronized with example  Hi Friends, I am beginner in java. what i... a particular resource we can lock the method using synchronized keyword. Then after... with example.Also explain object level lock and method level lock
Java Synchronized
Java Synchronized       The keyword synchronized entitled to the method or block is used...: Synchronized Methods: If you want a method of an object to be executed by one
doubt on synchronized block in java
doubt on synchronized block in java  Hi ! some people are feeling... am a beginner.I am learning java with out any teacher.I need your valuable suggestinons.Today i am going to ask on synchronized block. the general form
synchronized Java Keyword
synchronized Java Keyword       The synchronized is a keyword defined in the java programming... relevant to a compiler in java programming language likewise the synchronized keyword
Synchronized Threads
. Java's synchronized is used to ensure that only one thread is in a critical... when the object has synchronized method code. By associating a shared resource... currently in synchronized method and released the lock means exits the synchronized
Java Interview Questions - Page 4
executes a synchronized method after it has acquired the lock for the method's... Java Interview Questions - Page 4       Question: What are synchronized methods
Is catch(){} block synchronized?
Is catch(){} block synchronized?  The code in catch(){} block behaves synchronized. In one block, if I do {write to file1; write to file2}, and in another {write to file2; write to file1}, they deadlock. Is this implicit sync
how to ajax components are synchronized
how to ajax components are synchronized  How to ajax components are synchronized   Hi, Ajax consist of set of technologies to get the data from server Asynchronously. It is the object of var xmlHttp; which
java method
java method  can we declare a method in java like this static { System.loadLibrary("nativelib"); } i have seen this in a java learning E book. i don't understand the static { } plz help me. what kind of method
Synchronized Threads
. Java's synchronized is used to ensure that only one thread is in a critical... when the object has synchronized method code. By associating a shared resource... currently in synchronized method and released the lock means exits the synchronized
Synchronized Threads
. Java's synchronized is used to ensure that only one thread is in a critical... when the object has synchronized method code. By associating a shared resource... currently in synchronized method and released the lock means exits the synchronized
pass method reference in java
pass method reference in java  How to pass method reference in Java
Using a synchronized block
Description: This tutorial demonstrate how to implement synchronized block. In multithreading application a synchronized block is used to acquires the lock for an object. Code: class Called 
java method - Java Beginners
java method  Plz help me on toString() in java  Hi Friend, The Object class provides the method toString() that returns the string..., the toString() method of the object is automatically called. Thanks
class method in java
class method in java  How to write a class method in Java?   You must read Java - Class, Object and Methods in Java
java method - Java Beginners
java method  i wanna explation about a method for example... Mail[] getAllMails(String userName) I WANNA EXPLATION ABOUT ABOVE METHOD CAN U... and Tutorials on Java Mail visit to : http://www.roseindia.net/javamail/ Thanks
Using throw in java method
Using throw in java method  using throw with method implies what
try catch method in java
try catch method in java  try catch method in java - when and how should i use the try and catch method in Java ?   Please visit the following links: http://www.roseindia.net/help/java/t/java-try.shtml http
Java making a method deprecated
Java making a method deprecated  java making a method deprecated In Java how to declare a method depricated? Is it correct to use depricated... or method in Java. Instead of it you can use alternatives that exists. Syntax
JAVA Method Wait
JAVA Method Wait       The Wait method in Java hold the thread to release the lock till... understand a code that helps you to understand  Java method Wait.In this Tutorial
Static Method in Java
Static Method in Java  Is Overriding Static methods possible in java? If Yes, How
getch() method in java
getch() method in java  does someone know how can i obtain the getch() function in java
Generic Method - Java Beginners
Generic Method  I want simple java program for Generic Method with explanation
Class and method question, Java?
Class and method question, Java?  Consider a Java class that you... Prompt string** and the following method: getValue displays the prompt and reads..., the method should display an error message and ask the user for a new value
java method - Java Beginners
java method  hi friends, Is there any default return type for methods in java?  There is no default return type in java, as a user you have to specify the return type even void
getch() method in java
getch() method in java  does someone know how can i obtain the getch() function in java?   Hi Friend, In java, you can use the method next() of Scanner class. Here is an example: import java.util.*; class Hello
Method overloading in java program
Method overloading in java program  How can we use method overloading in java program?   Method overloading:?In method overloading methods have same name but different type of parameters. Here is an example of method
Finalize method - Java Beginners
method, please can you suggest me a way out.   Hi method, finalize : Every class inherits the finalize() method from java.lang.Object the method... to the object exist the Object finalize method performs no actions but it may
Advertisements
 

 

 

DMCA.com