Home arrow static arrow Java Programming [Archive] - my thread prb
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - my thread prb
This topic has 9 replies on 1 page.

Posts:37
Registered: 7/31/04
my thread prb  
Aug 6, 2004 6:53 AM



 
plz can anybody tell me how i can make ProcessPy wait until ProcessPx generate x and ProcessPx it
 public class Begin {     public static int y;  public static int x;  public static void main(String[] args) throws IOException {    ProcessPx Px = new ProcessPx();    ProcessPy Py = new ProcessPy();     Py.start();    Px.start();}} /// class ProcessPx extends Thread {   int x;public void  run() {Begin.x=Integer.parseInt(JOptionPane.showInputDialog("Enter x value"));System.out.print("\nX is  :"+Begin.x);Begin.x=Begin.x*Begin.x;[b]// i want to tell other thread that the generate X is Succedd and ready[/b] }} /// This process generate y depend on the X value generated in previous processclass ProcessPy extends Thread {public  void run() {Begin.y=Integer.parseInt(JOptionPane.showInputDialog("Enter y value"));System.out.print("\nY is  :"+Begin.y); Begin.y=Begin.y+Begin.x;  [b]// how i can let this Thred wait  until the Previous thred Generate X[/b] System.out.print("\ny+x is  :"+Begin.y); }}
 

Posts:24,036
Registered: 2/3/03
Re: my thread prb  
Aug 6, 2004 7:10 AM (reply 1 of 9)



 
Look at Thread.join().
 

Posts:37
Registered: 7/31/04
Re: my thread prb  
Aug 6, 2004 7:17 AM (reply 2 of 9)



 
wat Thread.join(). ?? i see its not have job here
 

Posts:319
Registered: 11/8/00
Re: my thread prb  
Aug 6, 2004 7:21 AM (reply 3 of 9)



 
Look up Thread.jion in the API!

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html
 

Posts:319
Registered: 11/8/00
Re: my thread prb  
Aug 6, 2004 7:22 AM (reply 4 of 9)



 
**** why can't you edit these

That should have read: Thread.join() not Thread.jion
 

Posts:24,036
Registered: 2/3/03
Re: my thread prb  
Aug 6, 2004 7:32 AM (reply 5 of 9)



 
wat Thread.join(). ?? i see its not have job here

class ThreadJoinDemo implements Runnable {        public static void main(String[] args) {        System.out.println("Starting " + Thread.currentThread().getName());        Thread t = new Thread(new ThreadJoinDemo(), "New Thread");        t.start();        // Uncomment the next line to see how join causes the currently running        // thread to wait until the joined thread finishes...        // try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); }               System.out.println("Stopping " + Thread.currentThread().getName());    }        public void run() {        System.out.println("Starting " + Thread.currentThread().getName());        try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }        System.out.println("Stopping " + Thread.currentThread().getName());            } }


http://www.google.com/search?q=java+threads+tutorial&sourceid=opera&num=0&ie=utf-8&oe=utf-8
[url=http://java.sun.com/docs/books/tutorial/essential/threads/]Java's Thread Tutorial[/url]
http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html
http://www.cs.clemson.edu/~cs428/resources/java/tutorial/JTThreads.html
http://www-106.ibm.com/developerworks/edu/j-dw-javathread-i.html
 

Posts:24,036
Registered: 2/3/03
Re: my thread prb  
Aug 6, 2004 7:33 AM (reply 6 of 9)



 
As njadams stated, it's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of [url=http://java.sun.com/reference/api/index.html]documentation[/url] that you can even [url=http://java.sun.com/docs/index.html]download[/url] for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

http://java.sun.com/reference/api/index.html
http://java.sun.com/j2se/1.4.2/docs/api/
 

Posts:414
Registered: 4/29/03
Re: my thread prb  
Aug 6, 2004 8:10 AM (reply 7 of 9)



 
Give it something nice to play with while it's waiting...?

But, seriously, a more elegant way...
public class WaitDemo{    public static int y;    public static int x;     public static void main(String[] args) throws IOException    {        WaitObject pxWait = new WaitObject();         //Start the processes        ProcessPx Px = new ProcessPx (pxWait);        ProcessPy Py = new ProcessPy (pxWait);        Py.start();        Px.start();    }} public class WaitObject{    public boolean ready = false;} ///class ProcessPx extends Thread{    WaitObject pxWait;    ProcessPx (WaitObject newPxWait)    {        pxWait = newPxWait;    }     public void  run()    {        WaitDemo.x = Integer.parseInt (JOptionPane.showInputDialog ("Enter x value"));        System.out.print ("\nX is  :" + WaitDemo.x);        WaitDemo.x = WaitDemo.x*WaitDemo.x;         // Tell other thread that the generate X is Succedd and ready        synchronized (pxWait)        {            pxWait.ready = true;            pxWait.notify();        }    }} /// This process generate y depend on the X value generated in previous processclass ProcessPy extends Thread{    WaitObject pxWait;     ProcessPy (WaitObject newPxWait)    {        pxWait = newPxWait;    }     public  void run()    {        WaitDemo.y = Integer.parseInt (JOptionPane.showInputDialog ("Enter y value"));        System.out.print ("\nY is  :" + WaitDemo.y);        WaitDemo.y = WaitDemo.y + WaitDemo.x;         // Wait  until the Previous thred Generate X        synchronized (pxWait)        {            if (!pxWait.ready)                pxWait.wait();        }         System.out.print ("\ny+x is  :" + WaitDemo.y);    }}
Disclaimer: This code has not been tested as my #$@%#^ simple Java compiler is on the fritz...
 

Posts:24,036
Registered: 2/3/03
Re: my thread prb  
Aug 6, 2004 9:28 AM (reply 8 of 9)



 
One last note...

Prefer implementing Runnable to extending Thread. Think of Threads as the workers and Runnables as the job itself. You implement Runnable because your "unit of work" or whatever you want to call it IS-A Runnable -- that is, something that will do its job start-to-finish when you call run. You would only extend Thread if your class IS-A Thread -- that is, it has a reason to do everything Thread does, and you need to slightly add to or modify Thread's behavior.
 

Posts:37
Registered: 7/31/04
Re: my thread prb  
Aug 6, 2004 10:30 AM (reply 9 of 9)



 
im rell thankz you yawmark ...All my thakz
 
This topic has 9 replies on 1 page.