Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Threads and classes (http://www.chiefdelphi.com/forums/showthread.php?t=84869)

Robototes2412 01-04-2010 01:04

Threads and classes
 
Hello all.

How come when I make a class into a thread, I cannot use the class as an object anymore?

I'm calling it as

public Thread foo = new FooThread();

where FooThread has function bar() which printfs "FOOBAR".

Using threads, how can I call foo.bar() inside my main loop?

TubaMorg 01-04-2010 07:55

Re: Threads and classes
 
Consult this tutorial for how to use threads in Java:

http://java.sun.com/docs/books/tutor...l/concurrency/

spartango 03-04-2010 20:07

Re: Threads and classes
 
If FooThread extends thread, then you can still call foo.bar(), but you have to cast the pointer to the FooThread instance to be not a Thread but a FooThread.

So

Thread t = new FooThread();
//you can now start thread or do whatever
//ie
t.start();

//you cannot, however do this:
t.bar();
//Because not ever thread has bar() method. only FooThreads do.
//but this will work:
((FooThread) t).bar();

//Alternatively, you can create your pointer as a FooThread type as opposed to simply a thread
FooThread t = new FooThread();
//this way you can
t.start();
t.bar();

Robototes2412 03-04-2010 23:31

Re: Threads and classes
 
Oh, wow, I can't believe I missed that, XD

thanks


All times are GMT -5. The time now is 13:47.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi