由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Can Java thread return a value?
相关主题
java的接口runnable能这么 create thread 吗?
请教一个多线程的问题implements runable 和 extends thread
请问关于用threadPoolExecutor实现threadpool的问题?线程问题。
Re: connection pool另一个入门问题。
JDK HashMap 的 Entry class再问一个今天的面试题
FrameWork of Thread application 1java ,wait ,notify notifyall
ZT: 关于性爱的多线程问题研究(一)一道java题
问个Thread 的问题,java certificate里的what is the use of thread.yield()?
相关话题的讨论汇总
话题: thread话题: value话题: return话题: java话题: public
进入Java版参与讨论
1 (共1页)
g****n
发帖数: 18
1
Hello everyone,
Does anyone know in Java can a thread return a value, like a function can do?
Thanks!
c**g
发帖数: 274
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
发帖数: 23
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
发帖数: 17532
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
发帖数: 23
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
发帖数: 17532
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
发帖数: 737
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
发帖数: 737
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
发帖数: 23
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
发帖数: 23
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
发帖数: 17532
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
发帖数: 23
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
发帖数: 158
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!
1 (共1页)
进入Java版参与讨论
相关主题
what is the use of thread.yield()?JDK HashMap 的 Entry class
NoThread concurrencyFrameWork of Thread application 1
好虫,exploit等大牛请进--关于htmlunitZT: 关于性爱的多线程问题研究(一)
core java多线程一般面试什么问个Thread 的问题,java certificate里的
java的接口runnable能这么 create thread 吗?
请教一个多线程的问题implements runable 和 extends thread
请问关于用threadPoolExecutor实现threadpool的问题?线程问题。
Re: connection pool另一个入门问题。
相关话题的讨论汇总
话题: thread话题: value话题: return话题: java话题: public