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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


RAJWINDER
thread class
1 Answer(s)      4 years and 7 months ago
Posted in : Java Beginners


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.

Advertisement
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
Why the wait(),notify() should not avaialable in Thread class.
Why the wait(),notify() should not avaialable in Thread class.  wait(),notify(),notifyall() is available in Object class.We are using those method in Threading then why don't they put the 3 methods in thread class
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
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... Thread class to create thread. class ThreadClass extends Thread
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
Thread
Thread  class Extender extends Thread { Extender(Runnable run... :"); //new Thread(new Implementer()).start(); } } class Implementer... Implementer Thread is started: "); } } public class ThreadDemo { public static
Thread
Thread  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
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads   Hi Friend, Difference: 1)If you want to extend the Thread class
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads   Hi Friend, Difference: 1)If you want to extend the Thread class
thread
thread  can parent thread be dead if child thread is not dead
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
Thread
Thread  what is the use of thread
Thread
Thread  Thread Life Cycle
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread...(). Example : class RunnableThread implements Runnable { Thread thread
Thread
Thread  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
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
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
Difference between extends thread class vs implements runnable interface - Java Interview Questions
Difference between extends thread class vs implements runnable interface  Hi Friends, can you give difference between extending thread class... want to extend the Thread class then it will make your class unable to extend
Thread in java
Thread in java  which method will defined in thread class
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
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
Thread restart
Thread restart  hello,, can dead thread restart?   hii, A dead Thread can't restart not even by calling start() again on that object
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
java Thread
java Thread  what is purpose of Thread
Daemon thread
Daemon thread  what is need of daemon thread
Daemon thread
Daemon thread  what is need of daemon thread
Java thread
Java thread  How can a dead thread be restarted?  A dead thread cannot be restarted
Java thread
Java thread  What are the ways in which you can instantiate a thread
Java thread
Java thread  What is the difference between process and thread
Java thread
Java thread  What are the high-level thread states
 

 

 

DMCA.com