Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can Thread return values  RSS feed

 
Helen Choo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new in threads but based on what i read, to create a thread class, you need to extend from Thread class and do the normal constructer and have a run or start method inside(Is it a must?)Is it possible that a run or start method return a value to the object that calls it?
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you wanted to effectively return a String, I think this will work. You have to make the extra call to get the return value after run completes -
<code>
class A extends Thread {
private String[] s;
private int thrNum;
public A(int thrNum) {
this.thrNum = thrNum;
s = new String[thrNum];
}
public void run() {
...
s[thrNum] = // what ever you want returned
...
}
public String getReturnValue(int thrNum) {
return s[thrNum];
}
...
}
</code>
 
Yuri Gadow
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your only two options are Runnable and Thread, and the method signatures cannot, of course, be changed when overridden, the short answer is no. The long answer is very long, as there is a multitude of thread messaging techniques out there for complex problems (see Tools of Computing, from the guys who wrote High-Performance Java Platform Computing, an excellent read) and the more common (note this link was down, but you could get a cached version from google.) Hope these help.
(This google link doesn't want to work on UBB, for me anyway, search google for doug+lea+util.concurrent if needed.)
[This message has been edited by Yuri Gadow (edited June 21, 2001).]

fixed links with [ url][ /url] tags.

[This message has been edited by Marilyn deQueiroz (edited September 08, 2001).]
 
Helen Choo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The websites provided is unaccessible.
Does it means that threads cannot return a value.
Then how do I control the concurrent submission from the servlets to the database?
I have many servlets wanting to access the database. I want to control the servlets and the users so that the data obtained is the most-up-to-date, no data conflicts and of high performance.
How do I go about doing it?Is there a need to create threads or servlets will help me in the management of threads?
Pls help me in this
 
Yuri Gadow
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the links got a bit mangled, let's try that again:
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
http://sourceforge.net/projects/jhpc
You cannot change the signature of the start() method, and it is declared as void in both Thread and Runnable � so, no, in the strictest sense, a thread cannot return a value. To get around this, you can use other shared data structures, such as a monitor (an object with methods guarded by synchronized) or a database. Both of the above sites have source code which demonstrates some good solutions for this and quite a few other multithreading issues.
As for controlling concurrent access to a database by your Servlets, that is generally best left to the data logic. If you are using a Commercial Off The Shelf (COTS) database (such as Oracle or SQL Server), it should provide transaction support for you (called via a JDBC API for instance.)
If you do not have access to a transaction manager, then a simple solution would be to apply a synchronized (probably as an alternating reader/writer) Proxy or Adapter pattern to the class(es) that provide you with access to the database in the simple case. Or, if your needs are greater, you may wish to investigate a COTS transaction manager � but you would then need to provide resource manager implementations to work with it.
Is this helping, or am I misunderstanding?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of spawning a thread is to execute multiple pieces of code simultaneosly. It doesn't make any sense to spawn a thread, start it running, and then wait for it to finish before doing anything else. In that case, you might as well perform the action in the original thread (the thread you are in when you create the new thread).
P.S. You're not supposed to override start or invoke run directly. You override run and invoke start (which creates a new thread of execution and then invokes run).
Start contains all the logic for spawning the new thread.
 
Yuri Gadow
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good points. It certainly wouldn't make sense to spawn a single thread and then join it. It would make sense to spawn multiple threads and synchronize on a barrier - which I think is direction Helen is hinting at with thread return values.
Insofar as overriding start(), it certainly isn't a good idea � as you�d basically end up with a Runnable instead of a usable Thread. My (poorly stated) point was that if one were to put in a signature with a return type, it would be start() not run() since the former wraps the latter � but this hardly makes sense to say, so it's a bit of a moot point.
Perhaps I should rephrase the previous post a bit. Simply put, if you merely need synchronized access to a database from a number of Servlets, utilise the database's transaction functionality - not application layer thread logic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!