Home Discussion Discuss: Synchronized Threads

  • threadsmukesh July 1, 2011 at 11:09 PM

    hi explain me 'Share(String threadname)' this line

  • synchronizedAlaa August 19, 2011 at 8:39 AM

    I had a different output that both threads start to execute in the same time but when I comment the try block which has the sleep() method in side it I get this output as shown in the example. I know when it comes to threads there is nothing to be said is guaranteed but i don't think with this example!! we all know that synchronized methods or blocks give a nice orderly turn between threads like in this example if we comment the try block i.e sleep() method then the second thread won't start until the first thread's run() method completes, but when you put first thread to sleep it gives a chance to the second thread to start it's own run method and that what happened to me. Regards Alaa

  • Wrong program "class Share extends Thread"Avinash December 16, 2011 at 11:19 PM

    Hello your program above "class Share extends Thread" doesn't have any thread synchronization effect because lock is placed on the different object of the class. To make it work properly you need to make dispay() method static. Please make correction in that.

  • synchronized methodsjurijs January 7, 2012 at 10:09 PM

    It very interesting and usefull theme. But I think that we can synchronized methods as callback function. In this situation we can have mutex object and semaphore with lock. One method lock semaphore after that another method can read it and synchronized with first metod regardless of the time of his call. It may be usefull if we work with network or with great resource loading and don't knew delay time. It's true?

  • Output Not as mentionedCD February 15, 2012 at 3:00 AM

    I am not sure whether the above example (with synchronization block) have been executed or not. But, I tried executing them. First thing, it does not compile because of missing braces. Once i fixed that issue, it does not give desired result as posted, instead it prints randomly every time you execute but definitely not gives single time the posted output. And i think that was not the objective of this article. The last output that i got, am pasting it here: Thread One: This Thread Two: This Thread One: is Thread Two: is Thread One: a Thread Two: a Thread Two: synchronized Thread One: synchronized Thread One: variable Thread Two: variable Hope the author reads my post and post corrections.

  • about javabalaraju March 1, 2012 at 11:19 PM

    briefly gives the matter

  • ThreadingSushant April 16, 2012 at 5:49 PM

    In the above example "public void display(String threadN)", why threadN is passed to display? What is the use of that?

  • javaRaveesh Kumar April 28, 2012 at 1:37 AM

    This program is not synchronized

  • sync thread examples Andy May 9, 2012 at 3:10 AM

    Tried running your provided code and neither examples produced syncd output. Besides, there is a bug in the second example. One curly bracket is missing. Thread One: This Thread One: is Thread One: a Thread Two: This Thread Two: is Thread Two: a Thread Two: synchronized Thread Two: variable Thread One: synchronized Thread One: variable

  • Java monitor is not a "semaphore" Seshadri Rangaswami December 17, 2012 at 3:58 AM

    Java monitor is a "mutex" and not a "semaphore". Of course, mutex is a degenerated version of Semphore with only one lock.

  • Wrong use of synchronizeAndrei April 17, 2013 at 3:42 PM

    Your display() method should also be static in order for the synchronized keyword to make any difference. Also I would suggest this code for a better understanding of the synchronized keyword : class Shared { public String msg[]={"This", "is", "a", "synchronized", "variable"}; public synchronized void display(String threadN){ for(int i=0;i<=4;i++){ System.out.println(threadN+msg[i]); try{ Thread.currentThread().sleep(1000); }catch(Exception e){ System.out.println("ERROR !"); } } } } class ShareThread extends Thread{ private Shared shared; ShareThread(String threadname, Shared shared){ super(threadname); this.shared = shared; } public void run(){ shared.display(getName()); } } public class NewClass { public static void main(String[] args) { Shared shared = new Shared(); ShareThread t1=new ShareThread("Thread One: ",shared); t1.start(); ShareThread t2=new ShareThread("Thread Two: ",shared); t2.start(); } }

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Java Beginners

threads
threads  what are threads? what is the use in progarmming 

Java Interview Questions

Threads
 

Java Interview Questions

Threads
 

Java Beginners

Threads
 

Java Interview Questions

Threads
Threads   public class P3 extends Thread{ void waitForSignal() throws InterruptedException { Object obj = new Object(); synchronized (Thread.currentThread()) { obj.wait 

Java Beginners

threads
 

Java Beginners

threads
 

Java Beginners

threads and events
threads and events  Can you explain threads and events in java for me. Thank you.   Java Event Handling Java Thread Examples 

Development process Questions

threads in java
threads in java  how to read a file in java , split it and write into two different files using threads such that thread is running twice 

Java Interview Questions

Java threads
Java threads  What are the two basic ways in which classes that can be run as threads may be defined 

Java Beginners

disadvantage of threads
is the disadvantage of threads?   hello, The Main disadvantage of in threads... disadvantage of Threads. Let?s discuss the disadvantages of threads. The global... are advised to put the global variable access code into synchronized{} block. Many 

Java Beginners

Sync Threads
Sync Threads  "If two threads wants to execute a synchronized method in a class, and both threads are using the same instance of the class to invoke the method then only one thread can execute the method at a time." This is what 

Java Beginners

threads in java
threads in java  iam getting that the local variable is never read in eclipse in main classas:: class Synex4{ public static void main(String args[]){ Test1 ob1=new Test1(); //local variable never read 

Java Beginners

Synchronization on threads
synchronized method and block??   Hello Friend, Differences: 1)Synchronized blocks place locks for shorter periods than synchronized methods. 2)Synchronized... will be queued and are executed while Statements in the synchronized block are executed 

Java Beginners

Threads in realtime projects
Threads in realtime projects  Explain where we use threads in realtime projects with example 

Programming Tutorials

Synchronized Threads
Synchronized Threads      ... being corrupted by multiple threads by a keyword synchronized to synchronize them... and non-synchronized methods.   If two threads wants to execute 

Java Beginners

Coding for life cycle in threads
Coding for life cycle in threads  program for life cycle in threads 

Java Beginners

Explain about threads:how to start program in threads?
Explain about threads:how to start program in threads?  import...; Learn Threads   Thread is a path of execution of a program... more than one thread. Every program has at least one thread. Threads are used 

Java Beginners

Examples on threads and mulithreading.....
Examples on threads and mulithreading.....  Is any good examples on threads and Mulithreading...   Hi Friend, Please visit the following link: Thread Tutorial Thanks 

Java Beginners

java threads - Java Beginners
java threads  What are the two basic ways in which classes that can be run as threads may be defined 

General

Synchronized Threads
Synchronized Threads      ... being corrupted by multiple threads by a keyword synchronized to synchronize them... synchronized and non-synchronized methods.   If two threads wants 

General

Synchronized Threads
Synchronized Threads      ... being corrupted by multiple threads by a keyword synchronized to synchronize them... and non-synchronized methods.   If two threads wants to execute 

Java Beginners

Java Threads - Java Beginners
Java Threads  Why we use synchronized() method?  Hi Friend, We used synchronized() method to allow one thread to execute at a time.It allows the threads to wait for resources to become available and also notify 

Java Beginners

threads in java - Java Beginners
threads in java  what is the difference between preemptive scheduling and time slicing?   hi friend, In Preemptive scheduling, a thread... or the priority of one of the waiting threads is increased. While in Time Slicing 

Java-Tutorials

Daemon Threads
Daemon Threads       In Java, any thread can be a Daemon thread. Daemon threads are like a service providers for other threads or objects running in the same process as the daemon 

Mobile Applications Questions

threads & autorelease pool
threads & autorelease pool  How to set autorelease pool for NSThread method in Objective C?   [NSThread detachNewThreadSelector:@selector(yourMethod) toTarget:self withObject:nil]; - (void)yourMethod 

Java Beginners

Threads,Servlets - Java Beginners
Threads,Servlets  1)Is two Start mathods exist in one Thread Class? like create an object ThreadClass a= new ThreadClass; a.start(); a.start(); 2)How can u refresh a Servlet when new record is added to D.Base 

Java Beginners

multi threads - Java Beginners
using three threads. I want to declare variables which will be available to all the threads to access. Is there a way to declare the variables as global variables which will be available to all the threads. If so please send me the code 

Java Interview Questions

threads - Java Interview Questions
that will work even if many Threads are executing it simultaneously. Writing it is a black... interactions between Threads. You have to do it by logic. In a computer, something 

Java Beginners

interfaces,exceptions,threads
THE COMPLETE CONEPTS OF INTERFACES,EXCEPTIONS,THREADS   Interface...: Exception Handling in Java   Threads A thread is a lightweight process which exist within a program and executed to perform a special task. Several threads 

DMCA.com