Java synchronized method

Java synchronized method

Dear Friend,

I have written below program for synchronized method in java but it's not working plz 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(); } }

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();
    }
   }

Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
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
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
pass method reference in java
pass method reference in java  How to pass method reference in Java
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
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
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
Using throw in java method
Using throw in java method  using throw with method implies what
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
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
ModuleNotFoundError: No module named 'synchronized-set'
ModuleNotFoundError: No module named 'synchronized-set'  Hi, My... named 'synchronized-set' How to remove the ModuleNotFoundError: No module named 'synchronized-set' error? Thanks   Hi, In your
java sleep in main method
java sleep in main method  Hi, How to write Java program for sleeping in the main method? I want Java program to have sleep in main method. Try to share me the code examples. Thanks
java sleep in main method
java sleep in main method  Hi, How to write Java program for sleeping in the main method? I want Java program to have sleep in main method. Try to share me the code examples. Thanks
Static Method in Java
Static Method in Java  Is Overriding Static methods possible in java? If Yes, How
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
Is main method compulsory in Java?
Is main method compulsory in Java?  Hi, Can we have a executable class in Java without a main method? I am going to write simple Java program and there is main method. Is main method compulsory in Java? Thanks   Hi
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
method overriding in java
method overriding in java  program to compute area of square,rectangle,triangle,circle and volume of sphere,cylinder and perimeter of a cube using method overriding
method overriding in java
method overriding in java  program to compute area of square,rectangle,triangle,circle and volume of sphere,cylinder and perimeter of a cube using method overriding
what is ment by method signature in java.......
what is ment by method signature in java.......  what is ment by method signature in java.......   Hi Friend, The signature of a method is the combination of the method's name along with the number and types
Java Method with arrays
Java Method with arrays  My assignment is to write a java method..., write a program to test your method. my main method code is : public class...]; currentSmallestIndex = i; } } return currentSmallestIndex; } //main method
When is java main method called?
When is java main method called?  When is java main method called? Please explain the main method in Java with the help of code.   In a java class, main(..) method is the first method called by java environment when
customize method in java
customize method in java  hi there i want to create one customize method in which any no of parameters can be pass e.g the limit is 13 and if user wants to enter 3 inputs then that much can only be proceed so can i have solution
How to use charAt() method in Java?
How to use charAt() method in Java?  Hi, what is the correct way of using chatAt() method in Java? Thanks   The charAt() method in Java... at charAt() Method In Java. Thanks
Static Method in java with realtime Example
Static Method in java with realtime Example  could you please make me clear with Static Method in java with real-time Example
Java method Overriding
Java method Overriding       Below example illustrates method Overriding in java. Method overriding in java means a subclass method overriding a super class method. Superclass
Clone method example in Java
Clone method example in Java       Clone method example in Java programming language Given example of java clone() method illustrates, how to use clone() method. The Cloneable
method
method   how and where, we can define methods ? can u explain me with full programme and using comments
Java Script location.replace method
Java Script location.replace method  Hi, What does `Print `{<SCRIPT LANGUAGE="JavaScript"> location.replace("} & urlpop & {","BOTTOM") </SCRIPT>} this mean as this is written in an Lotus Script agent and I
java program on recursive method
java program on recursive method  in how many ways can you make change for one dollar(100 cents) using pennies(1-cent coins), nickels(5 cents), dimes(10 cents),and quarter(25 cents)? the coins must add up to the exact total
Example of Hashset iterator method in java.
Example of Hashset iterator method in java. In this exmple of HashSet class,  you will see the use of iterator() method. It is used for traversing all...)); System.out.println("Element before clear method : " + obHashSet
Java Thread : run() method
Java Thread : run() method In this section we are going to describe run() method with example in java thread. Thread run() : All the execution code is written with in the run() method. This method is part of the Runnable interface
Final method in java
Final method in java In this section we will learn about Final method in java. Final  keyword in java is used with class, variable and methods. If final is used with method that method will not be overridden in subclass, if final
method
method  can you tell me how to write an abstract method called ucapan() for B2 class class A2{ void hello(){ system.out.println("hello from A2"); }} class B2 extends A2{ void hello(){ system.out.println("hello from B2
Example of Hashset size() method in java.
Example of Hashset size() method in java. The size() is a method of HashSet class. It is used to calculte the size of HashSet. The size of method is equal to total number of elements in HashSet Code: 
sleep method in thread java program
sleep method in thread java program  How can we use sleep method... example ,we have used sleep method. we are passing some interval to the sleep method .After that interval thread will awake.   public class test
Method Overloading Example In Java
Method Overloading Example In Java In this section we will read about overloading in Java. Method overloading in Java is achieved due to the Java supports... with the same name. The concept of method overloading allows the Java programmer
Java draw triangle draw method?
Java draw triangle draw method?  hi how would i construct the draw method for an triangle using the 'public void draw (graphics g ) method? im...; Here is an example that draws a triangle using drawPolygon method. import
Java hashmap clear() method example.
Java hashmap clear() method example. This tutorial is based on clear() method of java HashMap class. It removes all values from HashMap. Code: ...;EE"); /*Display Element before clear method */ System.out.println
Input / Output in Easiest Method in Java
Input / Output in Easiest Method in Java  How to input from keyboard, both strings and characters and display the result on the screen with the easiest method in Java?   Hi Friend, Try the following code: import
Method in Java
Method in Java       In this section, we will explore the concept of method in the reference of object... the type of value returned from a method. It can be a  valid Java type
Example of toArray() method of hashset in java.
Example of toArray() method of hashset in java. The toArray() is a method of HashSet in java. It is used to create array of object. It is also used...; HashSetToArray .java package net.roseindia; import java.util.HashSet
Java Thread : yield() method
Java Thread : yield() method In this section we are going to describe yield() method with example in java thread. yield() Method: When you want to stop...() method of Thread. public static void yield() - This method pauses
Example of contains method of hashset in java.
Example of contains method of hashset in java. The contains() is a method of hashset. It is used for checking that the given number is available..., it returns true otherwise false. Code:  HashSetToArray .java

Ads