Skip to main content

Thread Communication

thread communication deals the problem that if one thread leaves the monitor then how it will tell to the waiting thread that it is now free to get the monitor. java provide interprocess communication machenism through wait() ,notify() and notifyAll() methods. these method are implemented in the object class as final.


notify()
It called by the monitor owner thread to wakes up a single thread that is waiting for this monitor object. If more then one, threads are waiting for this monitor then one of them is chosen to be awakened. The choice is arbitrary.

notifyAll()
It called by the monitor owner thread to wakes up multiple threads waiting for this monitor object.

wait()
Causes current thread to wait until another thread invokes the notify () method or the notifyAll() method for this object. It throws InterruptedException

wait(long timeout)              
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
it throws InterruptedException

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object.

//Thread communication 
class Common
{
    synchronized void hello1(int i,String s)
    {
        System.out.println(i+" Hello1 : "+s);
        notifyAll();
        if(i<10)
        try{wait();}catch(Exception e){}
    }
    synchronized void hello2(int i,String s)
    {
        
        System.out.println(i+" Hello2 : "+s);
        notifyAll();
        if(i<10)
        try{wait();}catch(Exception e){}
    }
}
class MyThreadA extends Thread
{
    Common c=null;
    MyThreadA(Common c)
    {
        this.c=c;  
    }
    public void run()
    {    
        for(int i=1;i<=10;i++)
        c.hello1(i,"MyThreadA ");
    } 
}
class MyThreadB extends Thread
{
    Common c=null;
    MyThreadB(Common c)
    {
        this.c=c;  
    }
    public void run()
    {    
        for(int i=1;i<=10;i++)
        c.hello2(i,"MyThreadB ");
    }    
}
class ThreadCommunication
{
    public static void main(String ar[])
    {
        Common obj=new Common();
        MyThreadA t1=new MyThreadA(obj);       
        MyThreadB t2=new MyThreadB(obj);
        t1.start();
        t2.start();         
    }
}
/*OUTPUT
1 Hello1 : MyThreadA
1 Hello2 : MyThreadB
2 Hello1 : MyThreadA
2 Hello2 : MyThreadB
3 Hello1 : MyThreadA
3 Hello2 : MyThreadB
4 Hello1 : MyThreadA
4 Hello2 : MyThreadB
5 Hello1 : MyThreadA
5 Hello2 : MyThreadB
6 Hello1 : MyThreadA
6 Hello2 : MyThreadB
7 Hello1 : MyThreadA
7 Hello2 : MyThreadB
8 Hello1 : MyThreadA
8 Hello2 : MyThreadB
9 Hello1 : MyThreadA
9 Hello2 : MyThreadB
10 Hello1 : MyThreadA
10 Hello2 : MyThreadB
*/

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd

Write a code for servlet which will display all the fields of Employee table in tabular form.MCS-051,Dec09-3a

import javax.servlet.*; import java.io.*; import java.sql.*;   import javax.servlet.http.*;   public class Employee extends HttpServlet   {   public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,      ServletException   {   res.setContentType("text/Html");   PrintWriter pw = res.getWriter();   try   {   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   Connection con=DriverManager.getConnection("jdbc:odbc:dsn");   Statement st=con.createStatement();   ResultSet rs=st.executeQuery("select emp_id,emp_name,emp_dob,mob_no,email from employee");   String tr="";   while(rs.next()){   tr=tr+"<tr>";   tr=tr+"<td>"+rs.getString(1)+"</td>";   tr=tr+"<td>"+rs.getString(2)+"</td>";   tr=tr+"<td>"+rs.getString(3)+"</td>";   tr=tr+"<td>"+rs.getString(4)+"</td>&