Solved

How to use Thread

Posted on 2003-11-20
10
322 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
[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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Accepted Solution

by:
applekanna earned 25 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
 
LVL 3

Expert Comment

by:applekanna
ID: 9794729
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 25 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

752 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