Solved

How to use Thread

Posted on 2003-11-20
10
280 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 25 total points
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:applekanna
Comment Utility
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
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
Comment Utility
must write another class for to use Thread?
can i write internal class and extends Thread? or implement Runnable?

pts = 50
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:zeBes
Comment Utility
sorry, forget to add.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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
Comment Utility
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 25 total points
Comment Utility
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
Comment Utility
Thx for the points :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now