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



Home Answers Viewqa Java-Beginners Project Question No. 2B : 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.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


RAJWINDER
Project Question No. 2B : 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.
1 Answer(s)      4 years and 6 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 3:50 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();
}
}

For more information, visit the following links:

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

Thanks


Related Tutorials/Questions & Answers:
Java incrementor decrementor thread - Java Beginners
Thread is Incrementor and has one variable cnt1 with initial Value 0. Incrementor thread increments value of cnt1 by 1 each time. The other thread is Decrementor... value of cnt2 by 1 each time. - Incrementor thread increments value of cnt1
thread class - Java Beginners
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... each time. - Incrementor thread increments value of cnt1 by one
Advertisements
Thread Priorities
priorities in the form of integer value to each of its thread to determine...("My Thread 1");     MyThread2 m2=new MyThread2("My Thread 2");   } }  ... Thread Name :My Thread 1 Thread Name :My Thread 2
Thread Priorities
in the form of integer value to each of its thread to determine the execution...  The minimum priority of any thread (an int value of 1.... If at the execution time a thread with a higher priority and all other threads
Thread Priorities
priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread
what is a thread initial state? how to implement it?
what is a thread initial state? how to implement it?  what is a thread initial state? how to implement
Java Thread
threads for concurrent execution. Each thread has priority. You can also set priority to threads. Thread having higher priority will execute first..._PRIORITY (constant value 1). Thread class provides you 3 priority - MAX_PRIORITY
how to value of one variable pass in other variable in revetse order
how to value of one variable pass in other variable in revetse order  Like variable a=367 b must have valuev763
Thread
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 should wait for all the threads to be completed and it should be display final sum
Thread
Thread  My question is why it is needed to pass this keyword 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
Thread
Thread  What is multi-threading? Explain different states of a thread... a program or a process to execute many tasks concurrently (at the same time... processor system. States of Thread: New state ? After the creations of Thread
returning a value from Threads
returning a value from Threads  Hello I have worker pattern that uses ExecutorService to schedule Thread execution but getting stuck at some point when returning a value using Future.I have code snippet below: ExecutorService
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
thread
thread  can parent thread be dead if child thread is not dead
Thread
Thread  what is the use of thread
Thread
Thread  Thread Life Cycle
Thread
Thread  will this code work..? class A extends Thread { public void run() { for(int i=0;i<5;i++) system.out.println("i=" + i); } public static void main(string args[]) { A a = new A(); Thread t
Thread
Thread  Explain two ways of creating thread in java. Explain at three methods of thread class.   Java Create Thread There are two main... Create Thread There are two main ways of creating a thread. The first is to extend
Synchronized Threads
increases or decreases value of the variable "count"  by 1... given time, only one thread can hold the lock and thereby have access... synchronized access to the resource. Only one thread at a time can access the shared
Synchronized Threads
increases or decreases value of the variable "count"  by 1... given time, only one thread can hold the lock and thereby have access... synchronized access to the resource. Only one thread at a time can access the shared
Thread
Thread  class Extender extends Thread { Extender(Runnable run...(); } public void run(){ System.out.println("Extender Thread is Started :"); //new Thread(new Implementer()).start(); } } class Implementer
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
Thread
Thread  why we need threads? why we need Multithreads? with code and real time example
thread
thread  Hi what are the threads required to excute a programe except main threads? Thanks kalins naik
Creation of Multiple Threads
 Thread 1");     MyThread m2=new MyThread("My Thread 2");   } } Output... Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My
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... problems. 2)Extending the thread will give you simple code structure in comparison
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... problems. 2)Extending the thread will give you simple code structure in comparison
Java Thread : setDaemon() method
Thread, daemon threads are used to perform services for user threads. You can... daemon thread or not by providing value true or false. It tells the JVM to make... are setting  thread 1 as daemon thread. public class ThreadSetDaemon implements
you pass a variable by value.
you pass a variable by value.  How do you pass a variable by value?   Hi friends, Just like in C++, put an ampersand in front of it, like $a = &$b /*--------------Pass By value----------------*/ function add
Java Thread class
Java Thread Class is a piece of the program execution Java has multithreading facility. It allows multiple works(threads) to run at a time...) { thread1 t1 = new thread1(); t1.start(); } } Output This is thread 1
Synchronized Threads
increases or decreases value of the variable "count"  by 1... given time, only one thread can hold the lock and thereby have access... synchronized access to the resource. Only one thread at a time can access the shared
Advertisements
 

 

 

DMCA.com