• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call join() method for an object in some other class other than from main  RSS feed

 
shesh anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* All i need is to call in this program is the join method inside either class DivisionEven or DivisionOdd so that first DivisionEven executes(say) an then after its completion DivisionOdd executes...it definitely can be either way round but all i need is to know rather learn how to do it except in main */

package com.packages.multithreading;

class DivisionEven implements Runnable

{
Thread t;

DivisionEven()

{
t = new Thread(this,"\nDivision Thread ");
System.out.println("\nDivision Thread "+t);
t.start();
}
public void run()
{
int j;
try{
for(int l=1;l<20;l++)
{
j = l%2;
if(j==0)
System.out.println("\t"+l);
Thread.sleep(500);
}
}catch(Exception ie)
{
ie.printStackTrace();
}
}
}

class DivisionOdd implements Runnable
{
Thread t;
DivisionOdd()
{
t = new Thread(this,"\nDivision Odd Thread ");

System.out.println("\nDivision Odd Thread "+t);

t.start();
}
public void run()
{
int j;
try{
for(int l=1;l<20;l++)
{
j = l%2;
if(j!=0)
System.out.println("\t"+l);

Thread.sleep(500);
}
}catch(Exception ie)
{
ie.printStackTrace();
}
}
}

public class MultipleThreadLaunch
{
public static void main(String args[])
{

DivisionEven divEven = new DivisionEven();

DivisionOdd divOdd = new DivisionOdd();

System.out.println("Thread Even division is Alive "+divEven.t.isAlive());
System.out.println("Thread Odd division is Alive "+divOdd.t.isAlive());
try
{
System.out.println("Waiting for threads to finish...");
divEven.t.join();

}catch(InterruptedException ie)
{
System.out.println("Main Thread Interrupted");

}

System.out.println("Thread Even division is Alive "+divEven.t.isAlive());
System.out.println("Thread Odd division is Alive "+divOdd.t.isAlive());

System.out.println("Main Thread Exiting ");

}
}
 
shesh anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a loc that i missed...

{
System.out.println("Waiting for threads to finish...");
divEven.t.join();
divOdd.t.join();
}
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a bit hard to understand what your getting at but I'll try (I don't understand the relevance or see the problem with the 'outside of main' bit do you mean the main method or the main thread ??) ...

First some 'oddities' I think a problem is starting the threads in the constructors for your two objects in my opinion this is asking for trouble its could from to seperate thread object creation and thread start. Technically you can start running before your constructor completes for one, its usually could form to do object creation only in a constructor.
Second although its uaully a good idea to implement runnable rather than extend thread as you have a thread memember this seems counter productive in this case i would extend thread for your two objects this will make the code a bit cleaner (normally I choose to implement runnable).

To make your threads start one after another as the code is written the join should be between the two constructors but I suspect you might be better rejigging the code as I have suggested and then reposting. Currently your code fires off two threads and waits for one of them to stop 'divEven' so in your code case you wanted your divodd creation after the join and then probably another join on divodd if you want to be sure its completed BUT again i strongly suggest you redesign your code again.

In describing these problems its often useful to say what output you expect or would like i.e. what the System.out's print.
 
shesh anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure it's outside main thread (my sincere apologies)...and i did not exactly get what you tried to convey when you pointed to use the join between the constructors...what above code does is ..currently it's printing
1
2
3
4
.
.
.
48

instead what i really want is

1
3
5
7
.
.
19

and then

2
4
6
.
.
48

now if you could help me with your version of code where a join method makes to wait and after the called one has finished execution then later one should perform its operation. Hopin to see an answer
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way would be for the main class to manage the two threads:

There's really no point to threads at all in a setup like that.

To start them both and let them fight it out, more like this:

Now we have to imagine PrintEven's constructor saving the Thread argument as a member variable, and its run method doing the join() before looping. Let us know if that works!
[ August 10, 2007: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!