[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Thread question...

Posted on 2004-09-02
8
Medium Priority
?
252 Views
Last Modified: 2010-03-31
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
Comment
Question by:iismatt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11962689
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11962718
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
 
LVL 18

Expert Comment

by:armoghan
ID: 11962913
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
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!

 
LVL 35

Expert Comment

by:TimYates
ID: 11962919
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11962922
bah...too slow... ;-)
0
 
LVL 1

Author Comment

by:iismatt
ID: 11963051
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
 
LVL 35

Accepted Solution

by:
girionis earned 400 total points
ID: 11963131
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11963138
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question