Link to home
Start Free TrialLog in
Avatar of zeBes
zeBes

asked on

How to use Thread

in my app, i have 3 methods, i wanted them to run together..

public class APP{

  public APP{
    doThis();
   doThat();
   doIt();
  }

  void doThis(){//some code}
  void doThat(){//some code}
  viod doIt(){//some code}
}

so, how am i suppose to make all this three method run at the same time using Thread?
ASKER CERTIFIED SOLUTION
Avatar of applekanna
applekanna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of applekanna
applekanna

This is the main prg

public final class ThreadDemoTester {
public static void main(String[] args) {
ThreadDemo1 demo1 = new ThreadDemo1();
ThreadDemo2 demo2 = new ThreadDemo2();
ThreadDemo3 demo3 = new ThreadDemo3();

demo1.start();
demo2.start();
demo3.start();
}
}

Now all three thread will be runing.

Hope this helps
Cheers!

You could study this :
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

You could also try this :

1st Program :

public class Producer extends Thread {
    private CubbyHole cubbyhole;
    private int number;

    public Producer(CubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            cubbyhole.put(i);
            System.out.println("Producer #" + this.number
                               + " put: " + i);
            try {
                sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
    }
}

-----------------------------------------------------------

2nd Program :

public class Consumer extends Thread {
    private CubbyHole cubbyhole;
    private int number;

    public Consumer(CubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        int value = 0;
        for (int i = 0; i < 10; i++) {
            value = cubbyhole.get();
            System.out.println("Consumer #" + this.number
                               + " got: " + value);
        }
    }
}

-----------------------------------------------------------

Main Program :

public class ProducerConsumerTest {
    public static void main(String[] args) {
        CubbyHole c = new CubbyHole();
        Producer p1 = new Producer(c, 1);
        Consumer c1 = new Consumer(c, 1);

        p1.start();
        c1.start();
    }
}

Hope it helps . . .
Avatar of zeBes

ASKER

must write another class for to use Thread?
can i write internal class and extends Thread? or implement Runnable?

pts = 50
Avatar of zeBes

ASKER

sorry, forget to add.
You can, but be careful: innerclasses can't easily access the data around.

You might extend your own class from Thread, but .... how is "run()" to know that it must do something else
than the previous Thread you started or the next one: run() takes no parameters.

It is very difficult to write a single Thread class that can find out what to do, not conflicting with its brothers and sisters.
In general we write a Thread class with a clear name so everybody understands its special purpose.
;JOOP!
Avatar of CEHJ
If the inner classes are not anonymous, then they have access to the data of the outer class, so you can do this, although you must beware of synchronization on variables:

public class App {
      int i = 10;

      public App() {
            MethodExecutor m1 = new MethodExecutor() {
                  public void run() {
                        doThis();
                  }
            };
            MethodExecutor m2 = new MethodExecutor() {
                  public void run() {
                        doThat();
                  }
            };
            MethodExecutor m3 = new MethodExecutor() {
                  public void run() {
                        doIt();
                  }
            };
            m1.start();
            m2.start();
            m3.start();
      }

      void doThis() { System.out.println("doThis() called. i = " + i); }
      void doThat() { System.out.println("doThat() called. i = " + i); }
      void doIt() { System.out.println("doIt() called. i = " + i); }



      class MethodExecutor extends Thread {
            public void run() {
            }
      }

      public static void main(String[] args) {
            new App();
      }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thx for the points :)