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.

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 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
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  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  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  Thread Life Cycle
Thread
Thread  what is the use of thread
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
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
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  why we need threads? why we need Multithreads? with code and real time example
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  Hi what are the threads required to excute a programe except main threads? Thanks kalins naik
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
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
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 : 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
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
Thread scheduling
on the basis of their priority relative to other Runnable threads. The thread... is started, Java makes the lower priority thread wait if more than one thread exists...Thread scheduling  What is the algorithm used in Thread scheduling
how to assign javascript variable value to a jsp variable
how to assign javascript variable value to a jsp variable  how to assign javascript variable value to a jsp variable
Java :Thread Synchronization
Thread 1 : variable Thread 2 : This Thread 2 : is Thread 2 : a Thread 2... that at a time one thread would be able to use the resource. To overcome...("Thread 1 : "); t1.start(); Sync t2=new Sync("Thread 2 : "); t2.start
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 Creation
. Thread a: 0 Thread b: 0 Thread a: 1 Thread b: 1 Thread... by the thread. Create an instance of this subclass. This subclass may call...; value=0;   start();     }  
Main Thread and Child Thread
1 this thread is main 1 this thread is Child 2 this thread is Child 3 this thread is Child 2 this thread is main 3 this thread is main... and Child Threads used in Programming. Main thread is automatically created
javascript variable value insertion in DB
javascript variable value insertion in DB  how can I insert javascript variable value into database using php
Thread concept
have one, two , three threads. At one time it runs one, two , three and on other...("Thread One is alive: "+ob1.t.isAlive()); System.out.println("Thread Two is alive...("Interrupted"); } System.out.println("Thread One is alive: "+ob1.t.isAlive
Thread priority in java
concurrently. In java each and every thread has priority , priority means which... value between 1 to 10, here 1 is minimum priority and 10 is maximum priority. Thread class defines some priority constant as follows: MIN_PRIORITY = 1 NORM
Inter-Thread Communication
can communicate with each-other. This way reduces the CPU?s idle time i.e.... Produce: 1 consume: 1 In this program, two threads "Producer"... is invoked through the "Producer" class which increments the variable "
Overview of Thread
within a program. Each thread has its own local variables, program counter... of execution may be associated with a single process. Thus a process that has only one..., it firstly execute the main() method runs in a one thread, called the main
Java :Thread Join
Thread 1 Thread 2 Thread 3 Thread 3 Thread 3 Thread 2 Thread 2... milliseconds for the thread to die. A timeout of 0 means to wait forever. join(long... { ThreadJoin j1 = new ThreadJoin("1"); ThreadJoin j2 = new ThreadJoin("2
Java Sleep Thread
public void run() { for (int i = 1; i <= 4; i++) { if (i % 2 == 0... 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
java thread - Java Beginners
java thread  PROJECT WORK: Create a application using thread...: 1:customerAccount.java 2:TransferManager.java 3:AccountManager.java... transfer . Create two threads and initiate the execution of both the threads . Display
if i run this prog, i got exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Maxof2.main(Maxof2.java:9), whats the resolution for ths?
int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); if(i...if i run this prog, i got exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Maxof2.main(Maxof2.java:9), whats the resolution for ths
Java Thread : isDaemon() method
Thread, daemon threads are used to perform services for user threads. You can... are testing threads for daemon thread. public class ThreadIsDaemon implements...Java Thread : isDaemon() method In this section we are going to describe
thread dump
thread dump  Hi, I wanted to understand the Locked/waiting state below in the java thread dump. Is it normal to have waiting on locked object monitor value to be same, in the snippet below both are<0x701dd1e0> i see a lot
Java Thread
;thread is a sequential path of code execution within a program. Each thread has...() is declared to be public, takes no arguments, has no return value, and is not allowed... Java Thread       A java
Java Exception Thread
Thread There are method to create thread 1)Extends the Threads Class... of this class is runnable 2. The Thread constructor is used to create an object... 1)Multithread are lightweight as compared to any processes. 2)Context Switching
Thread Constructors
that contains all other thread and groups and each subgroups can contain other groups and threads. All thread have only one thread group. And all thread groups (except the root thread group) belongs to exactly one parent thread group. Threads can
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
 

 

 

DMCA.com