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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for more on threads
http://java.sun.com/docs/books/tutorial/essential/threads/
for more examples
http://www.ecs.umass.edu/ece/wireless/people/emmanuel/java/java/threads/simple.html
http://www.cs.msstate.edu/~ioana/Courses/CS6163/threads/sld006.htm
http://www.baek.com.hk/baek/showroom/simplethread.htm
http://java.sun.com/docs/books/tutorial/essential/threads/
for more examples
http://www.ecs.umass.edu/ece/wireless/people/emmanuel/java/java/threads/simple.html
http://www.cs.msstate.edu/~ioana/Courses/CS6163/threads/sld006.htm
http://www.baek.com.hk/baek/showroom/simplethread.htm
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("Produc er #" + 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("Consum er #" + 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 . . .
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("Produc
+ " 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("Consum
+ " 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 . . .
ASKER
must write another class for to use Thread?
can i write internal class and extends Thread? or implement Runnable?
pts = 50
can i write internal class and extends Thread? or implement Runnable?
pts = 50
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!
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!
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();
}
}
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
void doThat() { System.out.println("doThat
void doIt() { System.out.println("doIt()
class MethodExecutor extends Thread {
public void run() {
}
}
public static void main(String[] args) {
new App();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thx for the points :)
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!