Redian新闻
>
Can Java thread return a value?
avatar
Can Java thread return a value?# Java - 爪哇娇娃
g*n
1
Hello everyone,
Does anyone know in Java can a thread return a value, like a function can do?
Thanks!
avatar
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!

avatar
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.

avatar
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?

avatar
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.

avatar
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;

avatar
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!

avatar
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;

avatar
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()
: {
: ...

avatar
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?

avatar
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;

avatar
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.

avatar
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!
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。