Hello everyone, Does anyone know in Java can a thread return a value, like a function can do? Thanks!
c*g
2 楼
no, if what you want to know is "can run() return a value", Runnable interface defines run() as void.
【在 g****n 的大作中提到】 : Hello everyone, : Does anyone know in Java can a thread return a value, like a function can do? : Thanks!
n*m
3 楼
Of course you can. But you have to define your own runnable (wrapper). do?
【在 c**g 的大作中提到】 : no, if what you want to know is "can run() return a value", : Runnable interface defines run() as void.
xt
4 楼
How? The only way I can think of is a wait/notify mechanism.
【在 n*m 的大作中提到】 : : Of course you can. But you have to define your own runnable (wrapper). : do?
n*m
5 楼
It is much similar to asynchronous call (may be I should say the same) I've said, create your own runnable. Also, I suggest you can have your own thread pool public interface MyRunnable { public int run (); } or public class MyThread { private Thread thread; public int run () { thread = new Thread(); thread.start(); ... return something; } } ... MyThread mt = new MyThread (); int result = mt.run(); wrap Thread running function in the implementation of
【在 xt 的大作中提到】 : : How? : The only way I can think of is a wait/notify mechanism.
xt
6 楼
What is your point doing this? What is the advantage over sth like this: public class MyRunnable { private Object _mutex = new Object(); private int _value = -1; public void run() { ... update(); } private void update() { syncrhonized( _mutex ) { _value=1; _mutex.notify(); } } public Object getLock(){ return _mutex; } public int getValue(){ return _value;} }
【在 n*m 的大作中提到】 : : It is much similar to asynchronous call (may be I should say the same) : I've said, create your own runnable. Also, I suggest you can have your own : thread pool : public interface MyRunnable { : public int run (); : } : or : public class MyThread { : private Thread thread;
r*s
7 楼
Would you please show me how you would like to use it if its supported. just curious
do?
【在 g****n 的大作中提到】 : Hello everyone, : Does anyone know in Java can a thread return a value, like a function can do? : Thanks!
r*s
8 楼
is the "something" calculated in the new thread? if yes, do you need to wait for the thread to finish the calculation before you can "return something" if not, what's the point of doing all this?
【在 n*m 的大作中提到】 : : It is much similar to asynchronous call (may be I should say the same) : I've said, create your own runnable. Also, I suggest you can have your own : thread pool : public interface MyRunnable { : public int run (); : } : or : public class MyThread { : private Thread thread;
n*m
9 楼
He just wants a thread can return a value, or say, needs a sort of asynchronous call. Your function is mutex operation. I don't think they are the same thing.
【在 xt 的大作中提到】 : : What is your point doing this? What is the advantage : over sth like this: : public class MyRunnable : { : private Object _mutex = new Object(); : private int _value = -1; : public void run() : { : ...
n*m
10 楼
It can be calculated in the new thread. My sample is a typical asynchronous call. So, you may get invalid value from the call since the new thread may not finish. However, there are many ways to get a "correct" value. Here is a sample public class MyValue { boolean ready=false; int realvalue; public boolean getReady() { return ready; } public void setReady (boolean ready) { this.ready = ready; } } ... MainThread: MyValue returnValue = myThread.run(); while
【在 r***s 的大作中提到】 : is the "something" calculated in the new thread? : if yes, do you need to wait for the thread to finish the calculation before : you can "return something" : if not, what's the point of doing all this?
xt
11 楼
With all due respect, I do not often call this kind of code "program". You are wasting your CPU time doing nothing. You could put a Thread.sleep(100) in the while loop. That could be somewhat acceptable, especially under such circumstances that the program is not mission critical i.e. requirement on programming correctness is not as high.
【在 n*m 的大作中提到】 : : It can be calculated in the new thread. My sample is a typical asynchronous : call. So, you may get invalid value from the call since the new thread may not : finish. : However, there are many ways to get a "correct" value. Here is a sample : public class MyValue { : boolean ready=false; : int realvalue; : public boolean getReady() { : return ready;
n*m
12 楼
Neither do I. But here is just a sample. I know somebody will jump out:-) No Kidding!
【在 xt 的大作中提到】 : : With all due respect, I do not often call this kind of : code "program". You are wasting your CPU time doing nothing. : You could put a Thread.sleep(100) in the while loop. : That could be somewhat acceptable, especially under : such circumstances that the program is not mission critical : i.e. requirement on programming correctness is not as high.
e*g
13 楼
guys, just learn the right way. Object.wait(), Object.notify(), Thread.join(), etc. or check EDU.oswego.cs.dl.util.concurrent, which will be in java 1.5 standard API. btw, do not call Thread.run(). that's a normal sync method call!