thread class
Create 2 Thread classes.One Thread is Incrementor and has one variable cnt1 with initial
Value 0. Incrementor threads increments value of cnt1 by 1 each time.
The other thread is Decrementor which has variable cnt2 with initial value 100.
Decrementor threads decrements value of cnt2 by 1 each time.
- Incrementor thread increments value of cnt1 by one and notifies the other thread
about this value
- The decrementor threads decrements value of its variable cnt2 by one and compares values of cnt1 and cnt2. If values of cnt1 and cnt2 are different then notifies the
Incrementor thread and above mentioned step is repeated.
- But if values of cnt1 and cnt2 are matching then following message is displayed
Matching Value : 50
- Now Decremented thread sets a flag as true and notifies the other thread and stops
its execution.
- As flag is set to true, Increment or thread understands that matching has occurred
and stops its execution
NOTE : In this example use of wait ( ) , notify ( ) and synchronize statements is
Expected.
View Answers
April 23, 2010 at 4:48 PM
Hi Friend,
Try the following code:
class Incrementor extends Thread{
int cnt1 = 0;
boolean done = false;
Decrementor decrementor;
public void run() {
while(!done) {
synchronized(this) {
cnt1++;
decrementor.notifyWait(cnt1);
try {
wait();
}
catch (InterruptedException e) {
}
}
}
}
public void setDecrementor(Decrementor decrementor) {
this.decrementor = decrementor;
}
public void notifyWait() {
synchronized(this) {
notify();
}
}
public void done(){
done = true;
}
}
class Decrementor extends Thread{
int cnt2 = 100;
int cnt1;
Incrementor incrementor;
boolean done = false;
public void run() {
while(!done) {
synchronized(this) {
try {
wait();
}
catch (InterruptedException e) {
}
cnt2--;
System.out.println(cnt1+"-"+cnt2);
if(cnt2 == cnt1) {
System.out.println("Matching Value : 50 ");
done = true;
incrementor.done();
}
incrementor.notifyWait();
}
}
}
public void setIncrementor(Incrementor incrementor) {
this.incrementor = incrementor;
}
public void notifyWait(int cnt1) {
this.cnt1 = cnt1;
synchronized(this) {
notify();
}
}
}
public class Count{
public static void main(String str[]) {
Decrementor dec = new Decrementor();
Incrementor inc = new Incrementor();
inc.setDecrementor(dec);
dec.setIncrementor(inc);
dec.start();
inc.start();
}
}
Hope that it will be helpful for you.
Thanks
Related Tutorials/Questions & Answers:
thread class - Java Beginners the following code:
class Incrementor extends
Thread{
int cnt1 = 0;
boolean...;
}
}
class Decrementor extends
Thread{
int cnt2 = 100;
int cnt1...
thread class Create 2
Thread classes.One
Thread is Incrementor
Java Thread class
Java
Thread Class is a piece of the program execution
Java has...
It is created by extending the
Thread class or implementing
Runnable
interface
Java
Thread Class Example
public
class thread1 extends
Thread {
@Override
Advertisements
Create Thread by Extending Thread by extending
Thread class in java.
Extending
Thread :
You can create
thread by extending
Thread class and then by creating instance
of that
class you can... are extending
Thread class to
create
thread.
class ThreadClass extends
Thread 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... the
Thread class and the second is to implement the Runnable interface.
Please
Thread to the
thread constructor eventhough we had created only one
thread and if you say we have added to point to the current
thread then why we have not added this in the following line
"s=s1"
Pls reply......
class MyThread extends
Thread
{
Thread Java :Thread MethodsJava :
Thread Methods
This section explains methods of
Thread class.
Thread Methods :
Thread class provides many method to handle different
thread... :
class RunnableThread implements Runnable {
Thread thread;
public
ThreadThread
class Extender extends
Thread
{
Extender(Runnable run... :");
//new
Thread(new Implementer()).start();
}
}
class Implementer... Implementer
Thread is started: ");
}
}
public
class ThreadDemo
{
public static
ThreadThread will this code work..?
class A extends
Thread
{
public... attaching
class object to
thread..?
There are some compilation errors in your code. Here is your modified code:
class A extends
Thread {
public
Thread.
Java
Thread Example
class ThreadExample{
static int...Thread Write a Java program to create three theads. Each
thread should produce the sum of 1 to 10, 11 to 20 and 21to 30 respectively. Main
thread threadthread can parent
thread be dead if child
thread is not dead
ThreadThread
Thread Life Cycle
ThreadThread what is the use of
thread ThreadThread What is multi-threading? Explain different states of a
thread... processor system.
States of
Thread:
New state ? After the creations of
Thread instance the
thread is in this state but before the start() method invocation
ThreadThread what happen when we call the Wait(),Notify() and NotifyAll() methods in the
Thread Thread in javaThread in java which method will defined in
thread class Thread Thread there are two threads running at a time.. when am updating a values in database. both
thread halt and stop for moment till it get updated into database... so i dnt want
thread to get halts for tht moment of period. whats
Main Thread and Child Thread and Child Threads used in Programming.
Main
thread is automatically created when program runs.
Child
Thread gets created by the main
thread .
Java Main
Thread Example
public
class mainchild implements Runnable {
Thread t1
classclass Is a
class a subclass of itself
Thread concept in advance friends. Happy new year!!!!!
class Newthread3 implements Runnable{
Thread t;
String name;
Newthread3(String threadname){
name=threadname;
t=new
Thread...
Thread concept Everytime when i run a multithread program it gives
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 class is a
class to represent a country in medal tally. It has an attribute of
country...
medals. In this
class, you should also define constructors, and assessor, mutator
methods.
Task 2
MedalTally.java is a
class to model a medal tally, containing
Exception in thread Exception in
thread Hi,
I have created a java file for sending a file to my mail. I am using mail.jar file. I am able to create .
class file... 2
class as mentioned below
SendMailTLSDFC$1.class
SendMailTLSDFC.class
Class Class, Object and Methods
Class : Whatever we can see in this world all
the things... is termed as
a
class. All the
objects are direct interacted with its
class Java Thread Priority() method.
Java
Thread Priority Example
public
class priority implements...
Java Threads run with some priority
There are Three types of Java
Thread...++)
System.out.println(x + " This is
thread "
+ Thread.currentThread
Java :Thread getPriority Example returns the priority of your
thread.
Example :
class ThreadGetPriority...Java :
Thread getPriority Example
In this tutorial you will learn how to get
thread priority in java
thread.
Thread getPriority() :
Thread scheduler uses
Java Thread Context.
The ThreadContext
class is required for
thread specific debugging information to be stored...
Thread Context
The
Thread Context is required by the current
thread from
the group
Thread Priorities to set
and get the priority of a
thread.
class MyThread1 extends
Thread...());
System.out.println("
Thread Priority :"+cur);
}
}
}
class MyThread2...
Thread Priorities
Java Current Thread class provides method to display the current running
thread. It is
static
Thread...
running
thread by calling currentThread() method.
class NewCurrentThread...Java Current
Thread
In this tutorial, we are using Thread.currentThread
Java :Thread setPriority Example are setting
thread priority 10
to the current
thread.
class ThreadSetPriority...Java :
Thread setPriority Example
In this tutorial you will learn how to set
thread priority in java
thread.
Thread setPriority() :
Thread scheduler uses
Thread Priorities_TO_REPLACE_4
class MyThread1 extends
Thread{
MyThread1(String s... :"+cur);
}
}
}
class MyThread2 extends
Thread{
MyThread2(String s...
Thread Priorities
Java threadJava thread How can a dead
thread be restarted? A dead
thread cannot be restarted
Java ThreadJava
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 (constant
value 10) to
MIN_PRIORITY (constant value 1).
Thread class provides..._TO_REPLACE_4
public
class SimpleThread extends
Thread {
private int c = 3...Java
Thread
In this tutorial we will discuss about Java
Thread.
Java
Thread Thread restartThread restart hello,,
can dead
thread restart?
hii,ADS_TO_REPLACE_1
A dead
Thread can't restart not even by calling start() again on that object
Java Thread : setDaemon() method are setting
thread
1 as daemon
thread.
public
class ThreadSetDaemon...Java
Thread : setDaemon() method
In this section we are going to describe setDaemon() method with example in java
thread.
Daemon
Thread :
In Java
Java threadJava thread What's the difference between a
thread's start() and run() methods
Java threadJava thread What is the difference between process and
thread Thread methodThread method What is the purpose of the wait(), notify(), and notifyAll() methods
Java threadJava thread What invokes a
thread's run() method
Java threadJava thread What are the ways in which you can instantiate a
thread Java threadJava thread What are the high-level
thread states
Demon threadDemon thread What is demon
thread? why we need Demon
thread?
... there are daemon
thread by killing them abruptly.Any
thread can be a daemon
thread... = window.adsbygoogle || []).push({});
http://www.roseindia.net/java/
thread/daemon
Thread schedulingThread scheduling What is the algorithm used in
Thread scheduling? Java uses fixed-priority scheduling algorithms to decide which
thread... on the basis of their priority relative to other Runnable threads. The
thread