1

I have two Threads classes "AddThread" and "ReadThread". The execution of these threads should be like this "AddThread should add 1 record and wait until ReadThread displays the record after that ReadThread should display that added record again AddThread should add another record" this process should continue untill all the records are added(REcords are accessed from LinkedList). Here is the code

class AddThread extends Thread
{
    private Xml_Parse xParse;

    LinkedList commonlist;

    AddThread(LinkedList commonEmpList)
    {
        commonlist = commonEmpList;     
    }

    public void run()
    {
        System.out.println("RUN");
        xParse=new Xml_Parse();
        LinkedList newList=xParse.xmlParse();       
        try
        {
            synchronized (this) {
            if(newList.size()>0)
            {
                for(int i=0;i<newList.size();i++)
                {
                    System.out.println("FOR");
                    commonlist.add(newList.get(i));
                    System.out.println("Added" +(i+1)+ "Record");

                }
                System.out.println(commonlist.size());
            }
            }
        }
        catch(Exception e)
        {

        }
    }
}



class ReadThread extends Thread
{
    LinkedList commonlist;

    ReadThread(LinkedList commonEmpList)
    {
        commonlist = commonEmpList;     
    }
    public void run()
    {
        try
        {
            synchronized (this) {


            System.out.println();
            System.out.println("ReadThread RUN");
        sleep(1000);
        //System.out.println("After waiting ReadThread RUN");
        System.out.println(commonlist.size());
            if(commonlist.size()>0)
            {
                for(int j=0;j<commonlist.size();j++)
                {
                System.out.println("Read For");
                System.out.println("EmpNo: "+((EmployeeList)commonlist.get(j)).getEmpno());
                System.out.println("EmpName: "+((EmployeeList)commonlist.get(j)).getEname());
                System.out.println("EmpSal: "+((EmployeeList)commonlist.get(j)).getEmpsal());

                }
            }
            }
    }
    catch(Exception e)
    {

    }
    }
}


public class MainThread
{
    public static LinkedList commonlist=new LinkedList();

    public static void main(String args[])
    {       
        AddThread addThread=new AddThread(commonlist);
        ReadThread readThread=new ReadThread(commonlist);
        addThread.start();
        readThread.start();
    }

}
1
  • Threads are only useful when they can run concurrently/independantly. Having one thread wait for another in step is a more complex and slower way of doing the same thing without threads. Sep 21, 2011 at 12:53

3 Answers 3

2

You'll need to learn how to effectively use wait() and notify().

See also:

2
  • Ya i need to learn, but any solution for this?? how to make a thread wait??
    – Aniketh
    Sep 21, 2011 at 12:56
  • @Aniketh solution is: use wait() and notify()
    – pap
    Sep 21, 2011 at 13:27
1

What about using a BlockingQueue with a capacity of 1? Use offer instead of add so that producer thread is blocked.

You might also consider using a Semaphore with one permit, making it a mutex.

0

You use join() and yield() to control flux. If you want the current thread to stop and wait until the new thread finishes the work,

t1.run()
t.join()

when t1 finishes t continues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.