Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use Thread

Posted on 2003-11-20
10
Medium Priority
?
331 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:zeBes
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Accepted Solution

by:
applekanna earned 100 total points
ID: 9794714
write your three mtehods in three differnt class as follows
public class ThreadDemo1 extends Thread {

public ThreadDemo1() {
}
public void run() { //this is the importnat method of any class that extends thread

//doThis() code here
}
}

same way doit for ThreadDemo2 , ThreadDemo3
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9794718
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!

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:Javatm
ID: 9794742
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 . . .
0
 

Author Comment

by:zeBes
ID: 9795567
must write another class for to use Thread?
can i write internal class and extends Thread? or implement Runnable?

pts = 50
0
 

Author Comment

by:zeBes
ID: 9795616
sorry, forget to add.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9795939
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!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796455
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();
      }
}
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 100 total points
ID: 9800644
1.) Must write another class for to use Thread?

- Yes much better and extendable !

2.) Can i write internal class and extends Thread? or implement Runnable?

- Yes you can but be carefull as " sciuriware " said can't easily access the data around.

   Also CEHJ said : although you must beware of synchronization on variables:

It's up to you on what to use.

Hope it helps . . .
JAVATM

0
 
LVL 3

Expert Comment

by:applekanna
ID: 9836439
Thx for the points :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

876 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