ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
How two different class threads communicate with each
other?. send example code.
 Question Submitted By :: Athishankar
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How two different class threads communicate with each other?. send example code.
Answer
# 1
By Using the concept called Inter Thread Communication
 
Is This Answer Correct ?    3 Yes 1 No
Siva Kumar G
 
  Re: How two different class threads communicate with each other?. send example code.
Answer
# 2
using wait() and notify() two different class threads
communicate with each other. 
For example:

public class LoggingThread extends Thread {
  private LinkedList linesToLog = new LinkedList();
  private volatile boolean terminateRequested;

  public void run() {
    try {
      while (!terminateRequested) {
        String line;
        synchronized (linesToLog) {
          while (linesToLog.isEmpty())
            linesToLog.wait();
          line = (String) linesToLog.removeFirst();
        }
        doLogLine(line);
      }
    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
    }
  }

  private void doLogLine(String line) {
    // ... write to wherever
  }

  public void log(String line) {
    synchronized (linesToLog) {
      linesToLog.add(line);
      linesToLog.notify();
    }
  }
}
 
Is This Answer Correct ?    0 Yes 0 No
Modi[achir Communication]
 
 
 
  Re: How two different class threads communicate with each other?. send example code.
Answer
# 3
Below I am showing the best example for ur Query.


class Rack
{
 int n;
 boolean avialable  = false;
 synchronized int get()
 {
  if(!avialable )
  try
  {
   wait();
  }
  catch(InterruptedException e)
  {  
   System.out.println("InterruptedException caught");
  }
  System.out.println("got:" + n);
  avialable  = false;
  notify();
  return n;
 }

 synchronized int put(int n)
 {
  if(avialable )
  try
  {
   wait();
  }
  catch(InterruptedException e)
  {  
   System.out.println("InterruptedException caught");
  }
  this.n = n;
  avialable  = true;
  System.out.println("put:" + n);
  notify();
  return n;
  }
}

class Producer implements Runnable
{
Rack k;
Producer(Rack k)
{
this.k = k;
new Thread(this, "Producer").start();
}
public void run()
{
int i = 1;
while(true)
{
k.put(i++);
}
}
}
class Consumer implements Runnable
{
Rack k;
Consumer(Rack k)
{
this.k = k;
new Thread(this, "Consumer").start();
}
public void run()
{
while(true)
{
k.get();
}
}
}
class ProducerConsumerProblem
{
 public static void main(String args[])
    {
	Rack k = new Rack();
	new Producer(k);
	new Consumer(k);
	System.out.println("press control - c to stop. ");
     }
}
 
Is This Answer Correct ?    0 Yes 0 No
Tulasi Prasad
 
  Re: How two different class threads communicate with each other?. send example code.
Answer
# 4
The threads in java follow Mutual exclusion( synchronized)
and co-operation; 

for co-operation they need to talk to each other by messages
saying " i am waiting in the wait q anyone can acquire the
lock of the object" or" i have acquired the lock now by
notify() to the last thread who called wait() or by
notifyall() to all the threads who are in the wait q"
 
Is This Answer Correct ?    1 Yes 0 No
Puneet Khanna
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
how session will be expired? Infosys4
What enableEvents() method do?  1
Can an anonymous class be declared as implementing an interface and extending a class?  1
what is sendredirect? TCS3
Explain, why the constructor is required in implemented class?  2
Which class should you use to obtain design information about an object  2
I have one Shopping cart application, i that i have selected some items, while clicking submit button by mistake i have clicked twice or trice, that time items are selected twice or trice. Actually i want only one copy of items but its selected twice or trice. So how can we avoid this problem? Honeywell2
Different types of modifiers? RoboCom3
in a constructor what happen if u call super and this in the same class? i know that it is not possible to call both in the same one? if we call what will happen? ITC-Infotech6
What is the order of method invocation in an Applet?  1
What is the return type of readLine() when end of a file is reached?  1
What is the difference between Trusted and Untrusted Applet ? IBM1
explain System.out.println IBM41
my method "abc" return array of interface "xyz" and "pqr" is abstract class implements abc and class "jkl" extends pqr My problem 1) when i call abc it retrun array xyz how can i do this hint xyz refer_xyz = new jkl(); but i can't create array. 2)I want to access method of jkl using reference of xyz??  1
A class can be a subclass of itself? Genesis2
How to convert String into primitive datatype.  4
Explain pass by reference and pass by value? Wipro5
what are class,constructor and primitive data types?  2
how many ways to create Thread and which one is good? runnable interface ot Thread class? Satyam3
What Method and class used for Connection pooling ? Wipro4
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com