• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Thread question...

I asked this question last night...

http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Java/Q_21116212.html

I want to be able to spawn a thread that spawns different class as well as my original.  Can I overload any of the methods?  How exactly do I spawn a thread that can run either one class, or another?
0
iismatt
Asked:
iismatt
1 Solution
 
cjjcliffordCommented:
Not sure what you are asking (couldn't infer anything from previous question...) but here's a go...

// objA is one runnable to run, objB is another runnable to run...
new Thread( new Runnable() { public void run() { objA.run(); objB.run(); } } ).start(); // runs objA and objB consequetively in a seperate thread.

// start objA and objB (both implement java.lang.Runnable) in seperate threads.
new Thread( objA ).start();
new Thread( objB ).start();
0
 
cjjcliffordCommented:
or you could do something like the following...

public class Test extends Thread {
    public Test( Runnable runnable ) {
        super( runnable );
    }
    public void run() {
        super.run();
        System.out.println( "Test's runnable..." );
    }
    public static void main( String[] args ) {
        new Test( new Runnable() { public void run() { System.out.println( "Runnable as argument" ); } } ).start();
    }
}
0
 
armoghanCommented:
Do you want to start different threads from a thread?
if yes..
make other classes runnable as well and start them from the main thread
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
Just make class A and class B implement Runnable, and give them both "run" methods

public class A implements Runnable
{
    public void run()
    {
        System.out.println( "A" ) ;
    }
}

public class B implements Runnable
{
    public void run()
    {
        System.out.println( "B" ) ;
    }
}

Then, just do:

A a = new A() ;
Thread t = new Thread( a ) ;
t.start() ;

or

B b = new B() ;
Thread t = new Thread( b ) ;
t.start() ;
0
 
TimYatesCommented:
bah...too slow... ;-)
0
 
iismattAuthor Commented:
Here, let me clarify exactly what I'm confused about ... i have...

   public void run() {
         movingRandRect mrr = new movingRandRect();
    }

    void runProg() {
         if (t_movement==null); {
              Thread t_movement = new Thread(this);
              t_movement.start();
         }
    }

On some instances, I want to initiate my movingTriange.class instead of movingRandRect.  How would I do that?
0
 
girionisCommented:
Have a switch or something and do something like:

public boolean triangle = false;

...
...
triangle = true;

public void run() {
         if (triangle)
         {
             // instatiate movingTriangle
             triangle = false;
         }
         else
         {
                movingRandRect mrr = new movingRandRect();
         }
    }
0
 
TimYatesCommented:
Create an interface for your classes you want to pass to the thread...

public interface MyShape
{
    public void doStuff() ;
}

implement MyShape in movingRandRect and movingTriange classes, then write a Thread class like this:

public class Mover extends Thread
{
    MyShape shape = new MyShape() ;
    public Mover( MyShape shape )
    {
        this.shape = shape ;
    }

    public void run()
    {
        shape.doStuff() ;
    }
}

Then, you can do:

    void runProg() {
         if (t_movement==null);
         {
              Mover t_movement = new Mover( new movingRandRect() );
              t_movement.start();
         }
    }

or

    void runProg() {
         if (t_movement==null);
         {
              Mover t_movement = new Mover( new movingTriange() );
              t_movement.start();
         }
    }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now