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




Have Programming Question? Ask it here!
 
 
 


RAJWINDER
thread class
1 Answer(s)      4 years and 4 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  Thread Life Cycle
Thread
Thread  what is the use of thread
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
Daemon thread
Daemon thread  what is need of daemon thread
Daemon thread
Daemon thread  what is need of daemon thread
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
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
Thread method
Thread method  What is the purpose of the wait(), notify(), and notifyAll() methods
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
Demon thread
Demon 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.../thread/daemon-threads.shtml
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
Thread scheduling
Thread 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
Java Thread
_PRIORITY (constant value 1). Thread class provides you 3 priority - MAX_PRIORITY... that a thread hold. Example : public class SimpleThread extends Thread...Java Thread In this tutorial we will discuss about Java Thread. Java Thread
Thread Priorities
. Lets see, how to set and get the priority of a thread.   class MyThread1...);       }   } }   class MyThread2 extends Thread{   MyThread2(String s){     super(s);     start... Thread Priorities     
daemon thread
daemon thread  hello, What is a daemon thread?   hello, These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly
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
Thread Priorities
the priority of a thread.   class MyThread1 extends Thread{   MyThread1...("Thread Priority  :"+cur);       }   } }   class MyThread2 extends Thread...());       System.out.println("Thread Priority  :"+cur);       }   } } public class ThreadPriority
Java :Thread getPriority Example
the priority of your thread. Example : class ThreadGetPriority implements Runnable...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 setPriority Example
to the current thread. class ThreadSetPriority implements Runnable { Thread...Java :Thread setPriority Example In this tutorial you will learn how to set thread priority in java thread. Thread setPriority() : Thread scheduler uses
Thread in Nutshell
Thread in Nutshell  Hi, I m confused about what is called a thread actually in a program. There are many answer to this question on the web...; Please go through the following link: Thread Tutorials
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
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 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 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
Advertisements
 

 

 

DMCA.com