Java Thread Synchronization

Java Thread Synchronization

View Answers

November 14, 2008 at 1:50 AM

Hi friend,

Code to solve the problem :

import java.io.*;
class Share extends Thread{
static String msg[]={"This", "is", "a", "synchronized", "variable"};
Share(String threadname){
super(threadname);
}
public void run(){
display(getName());
}
public synchronized void display(String threadN){
for(int i=0;i<=4;i++)
System.out.println(threadN+msg[i]);
try{
this.sleep(1000);
}catch(Exception e){}
}
}
public class SynThread1 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bufReader = new BufferedReader(isr);
System.out.println("Press key enter");
bufReader.readLine();
Share t1=new Share("Thread One: ");
t1.start();
System.out.println("Press key enter");
bufReader.readLine();
Share t2=new Share("Thread Two: ");
t2.start();
}
}

For more information on Java visit to :

http://www.roseindia.net/java/

Thanks









Related Tutorials/Questions & Answers:
Thread Synchronization in Java
Thread Synchronization in Java Sometimes, when two or more threads need shared... as thread synchronization. The thread synchronization is achieved through... having thread synchronization :ADS_TO_REPLACE_2 class Print { void call(String
Java :Thread Synchronization
Java :Thread Synchronization This section explains how to use concept of synchronization in java Thread. Thread Synchronization : . Java supports multi... of program. So for the multi-threaded application, synchronization of java
Advertisements
Java Thread Synchronization - Development process
Java Thread Synchronization  Hi,Please help me with this coding. I... press any key on the keyboard,then one thread must stop.When I press the keys second time,then the second thread must stop. Please provide me with the code  
Java synchronization
Java synchronization  What is synchronization and why is it important?  Synchronization is best use with the Multi-Threading in Java. Synchronization is the way to make our program safe. As we know when we have two
Java Synchronization
Java Synchronization  What is synchronization and why is it important?  Synchronization is best use with the Multi-Threading in Java. Synchronization is the way to make our program safe. As we know when we have two
java synchronization
java synchronization   What is synchronization and why is it important
Synchronization with Multithreading - Java Beginners
information on Thread visit to : http://www.roseindia.net/java/thread/SynchronizedThreads.shtml http://www.roseindia.net/java/thread/ Thanks...Synchronization with Multithreading   Hi Deepak, what
Synchronization in java with example
Synchronization in java with example In this section we will discuss about Synchronization in java. Since java is a multi-threaded language so, when two... synchronization. Synchronized keyword in java create a critical section in which a lock
Synchronization
Synchronization  what is the use of synchronization
Synchronization and Singlethreadmodel - Java Beginners
Synchronization and Singlethreadmodel   Hi Deepak, wat is d difference between singleThreadmodel and synchronization. Thank u in advance
Synchronization - Java Beginners
the following links: http://www.roseindia.net/java/thread/synchronization.shtml http...://www.roseindia.net/java/thread/SynchronizedThreads.shtml Thanks... then the accessing thread is going to get the object lock to the whole class or lock is given
Synchronization
Synchronization  i want code for synchronization using in jsp and servlets
synchronization
synchronization  what is about static method synchronization&instance method synchronization
Synchronization - Java Interview Questions
Synchronization  Dear Sir my doubt is If we cannot use synchronization for a class then how a vector class is called as synchronized
how the streams provide synchronization - Java Beginners
how the streams provide synchronization  How the java streams provides synchronization
java Thread
java Thread  what is purpose of Thread
Java thread
Java thread  How can a dead thread be restarted?  A dead thread cannot be restarted
Synchronization
Synchronization      ... efficient, but makes two kinds of problems: thread interference and memory consistency errors. By the synchronization tool we can avoid this problem. In other
Java thread
Java thread  What's the difference between a thread's start() and run() methods
Java thread
Java thread  What is the difference between process and thread
Java thread
Java thread  What invokes a thread's run() method
Java thread
Java thread  What are the ways in which you can instantiate a thread
Java thread
Java thread  What are the high-level thread states
Synchronization probleam - Java Server Faces Questions
Synchronization probleam   Hi friends My java multithreading... = new ClientWorker(server.accept(), textArea); Thread t = new Thread(w... //program terminates and thread exits try{ server.close
Java thread
Java thread  Why do threads block on I/O?   When a thread... and in that time some other thread which is not waiting for that IO gets a chance to execute.If any input is not available to the thread which got suspended for IO
Synchronization probleam - Java Server Faces Questions
Synchronization probleam  Hi friend my question is why...{ w = new ClientWorker(server.accept(), textArea); Thread t = new Thread(w); t.start(); } catch (IOException e
Thread in java
Thread in java  which method will defined in thread class
Java Thread : getState() Example
Java Thread : getState() Example This section explains how to get state of a thread  in java Thread. Thread  getState() : Suppose you want to know the state of the thread so for that Java Thread provides Thread.getState
Java Thread and Runnable
Java Thread and Runnable  What's the difference between Thread and Runnable types
Synchronization and interlocking
Synchronization and interlocking  What are the different level locking using the synchronization keyword
How to Differenciate Main Thread and Child Thread in Java
Main Thread and Java Child Thread. Please Suggest any example or online link...({});   Hi, There are two types of Thread used in Java Programming Language. These are Java Main Thread and the Child Thread. The Below reference
Thread - Java Beginners
the concept of thread, thread creation and use of threads in JAVA application?  Thread creation and use of threads in JAVA Java Resourcehttp://www.roseindia.net/java/thread/thread-creation.shtml
Java thread state
Java thread state  what is Static binding
how to destroy java thread
how to destroy java thread  how to destroy java thread?   This will help .. Shutting Down the Java Thread
Java thread lock
Java thread lock  What happens when a thread cannot acquire a lock on an object
Java thread
Java thread  What is the use of serializable
Java thread
Java thread  What is the difference between wait() and sleep method
Java thread
Java thread  What method must be implemented by all threads
Java thread
Java thread  Can we have run() method directly without start() method in threads
Describe synchronization in respect to multithreading.
Describe synchronization in respect to multithreading.  Hi, Describe synchronization in respect to multithreading. thanks,   Are you eager the search related to Java programming query. Let us check
thread - Java Beginners
thread  can i asko for programs in thread method that will allow user to input two separate names  Hi friend, For more information on Thread visit to : http://www.roseindia.net/java/thread/index.shtml Thanks
Java thread state
Java thread state  What is the difference between yielding and sleeping
Java thread state
Java thread state  Difference between sleep and suspend
Thread
Thread  Explain two ways of creating thread in java. Explain at three methods of thread class.   Java Create Thread There are two main ways of creating a thread. The first is to extend the Thread class and the second
Daemon thread - Java Beginners
information, visit the following link: http://www.roseindia.net/java/thread...Daemon thread  Hi, What is a daemon thread? Please provide me... thread which run in background. like garbadge collection thread. Thanks 
Java Thread
Java Thread In this tutorial we will discuss about Java Thread. Java Thread : A thread is light weight java program.JVM permits you to have multiple threads for concurrent execution. Each thread has priority. You can also set
Java Sleep Thread
Java Thread sleep() is a static method. It sleeps the thread for the given time in milliseconds. It is used to delay the thread. It is used in Applet or GUI programming for animation Java Sleep Thread Example public class
Java Thread
Java Thread Tutorials In this tutorial we will learn java Threads in detail. The Java Thread class helps the programmer to develop the threaded application in Java. Thread is simple path of execution of a program. The Java Virtual Machine
Java :Thread getPriority Example
Java :Thread getPriority Example In this tutorial you will learn how to get thread priority in java thread. Thread getPriority() : Thread scheduler uses thread priority concept to assign priority to the thread. A higher priority 
Thread
Thread  What is multi-threading? Explain different states of a thread.   Java Multithreading Multithreading is a technique that allows... processor system. States of Thread: New state ? After the creations of Thread

Ads