Create Thread...

Y

yffaria

Its possible create a class extended by thread and in this class, to
instance other class extended by thread too?

What problems I found when I do that?

How to create more functions of thread class in class extended by
threads?

Thx!
 
R

Robert Klemme

yffaria said:
Its possible create a class extended by thread and in this class, to
instance other class extended by thread too?

What problems I found when I do that?

How to create more functions of thread class in class extended by
threads?

You can't insert a class into the inheritance hierarchy after the fact.
You could do that in Ruby though...

You can either implement Runnable and let all other Runnables inherit
that or inherit Thread and make your thread classes inherit that.

Regards

robert
 
S

Shin

yffaria said:
Its possible create a class extended by thread and in this class, to
instance other class extended by thread too?

What problems I found when I do that?

How to create more functions of thread class in class extended by
threads?

I'd like to see the context of this question; it might be interesting.
But in Java language, you can not insert a layer in between exsiting
class hierarchy; with some tools, though, you can do that

-Shin
 
Y

yffaria

The context is: I have one class (not thread class), and in this class
I have two class extended by thread. In one of the thread class, I have
on method that I open one file and write something, and this is do by
the other class thread. I need do this by one thread. So when I
initialize my other thread all happen normaly, but after when I start
the thread, nothing happen.

Are you understand me?
 
M

Matt Humphrey

yffaria said:
The context is: I have one class (not thread class), and in this class
I have two class extended by thread. In one of the thread class, I have
on method that I open one file and write something, and this is do by
the other class thread. I need do this by one thread. So when I
initialize my other thread all happen normaly, but after when I start
the thread, nothing happen.

Are you understand me?

It's definately a challenge for me. You say that you have a class extended
by Thread, which means (to me, at least) that you have somehow made the
system java.lang.Thread class extend (subclass) your class. This is
extremely unlikely. I am more likely to believe there is a language problem
and that you really have a class that contains two objects whose classes
extend (are subclasses of) Thread. One thread opens a file and writes.

Overally, I'm still really not sure what you're asking. My questions to you
(to help you better) are:

Do you mean extends and not extended by?
Do you really need to extend Thread at all instead of supplying a Runnable?
Why do you think you need to have a thread that starts other threads?
What do you really want these threads to do?

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
Y

yuriff

My File.java is:

class File{
public File(int poolSize){
//start poolSize threads of the PoolThreads
}

private final class PoolThreads extends Thread{
public void run(){
...
exec(some parameters);
...
}
private int exec(some parameters){
...
new ProcThread().start();
...
}
}

public class ProcThread extends Threads{
...
}
}

What was wrong with him?
 
M

Matt Humphrey

My File.java is:

class File{
public File(int poolSize){
//start poolSize threads of the PoolThreads
}

private final class PoolThreads extends Thread{
public void run(){
...
exec(some parameters);
...
}
private int exec(some parameters){
...
new ProcThread().start();
...
}
}

public class ProcThread extends Threads{
...
}
}

What was wrong with him?

Well, this answers the first question in that you have classes that extend
Thread. It certainly is possible to have a thread that instantiates
another thread. This brings us to the question of whether doing so solves
the problem. I guess you are trying to make a thread pool, but it would
really help if you could say what you want this to do. A thread pool is not
a kind of thread itself, so it is probably not meaningful to have it extend
Thread. In addition, there are some well-known techniques for this--see
these links.

http://www.informit.com/articles/article.asp?p=30483&rl=1

http://www-128.ibm.com/developerworks/library/j-jtp0730.html

Also the latest versions (1.5) of Java appear to include a ThreadPool
system. I havn't used these, but look for "ThreadPoolExecutor" and you will
probably save yourself a huge amount of trouble.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top