Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multithreading, need help figuring out how to reach the parent 1

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I have the following example<br>
<br>
import java.lang.Thread;<br>
<br>
public class TCon<br>
{<br>
int MyNum;<br>
public static void main (String[] args)<br>
{<br>
Thread F = new RThread(&quot;T1&quot;, 250);<br>
Thread S = new RThread(&quot;T2&quot;, 750);<br>
<br>
F.start();<br>
S.start();<br>
<br>
try<br>
{ System.in.read(); }<br>
catch (java.io.IOException ioe)<br>
{ }<br>
F.stop();<br>
S.stop();<br>
}<br>
public synchronized void setnumber(int newnum)<br>
{<br>
MyNum = newnum;<br>
}<br>
public synchronized int getnumber()<br>
{<br>
return MyNum;<br>
}<br>
}<br>
class RThread extends Thread <br>
{<br>
private String message;<br>
private int duration;<br>
<br>
public RThread(String m, int d)<br>
{<br>
message = m;<br>
duration = d;<br>
}<br>
public void run()<br>
{ <br>
try<br>
{<br>
while(true)<br>
{<br>
System.out.println(message);<br>
try<br>
{<br>
sleep(duration);<br>
}<br>
catch (InterruptedException ie)<br>
{<br>
System.out.println(&quot;Interrupted&quot;);<br>
return;<br>
}<br>
}<br>
}<br>
finally<br>
{<br>
System.out.println(message+&quot; shut down&quot;);<br>
}<br>
}<br>
}<br>
<br>
(this is just an example i'm learning with)<br>
now what I want to be able to do, is have the thread be able<br>
to call the setnumber, and getnumber located in the main/parent thread. but i do not know how to get ahold of it.<br>
(in C++ i would not normally just make a class pointer of the parent type, and assign its location, but pointers dont exist in java, and so on) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Hi!<br>
<br>
You have two ways (minimum :)):<br>
Make the MyNum, setnumber and getnumber to static, and from the thread just call TCon.setnumber();<br>
static int MyNum;<br>
public static synchronized void setnumber(int newnum)<br>
public static synchronized int getnumber()<br>
<br>
Or create a TCon class in the main, modify the constructor of RThread, give an other parameter (TCon parent) to it, and store it a local reference, and when call: parent.setnumber();<br>
<br>
class RThread extends Thread {<br>
private String message;<br>
private int duration;<br>
private TCon parent;<br>
<br>
public RThread(String m, int d, TCon parent)<br>
{<br>
message = m;<br>
duration = d;<br>
this.parent=parent;<br>
}<br>
<br>
public static void main (String[] args) {<br>
TCon tc=new TCon();<br>
<br>
Thread F = new RThread(&quot;T1&quot;, 250, tc);<br>
Thread S = new RThread(&quot;T2&quot;, 750, tc);<br>
.....<br>
<br>
Good luck. Bye, Otto.<br>

 
hmm thanks, Just the idea of thinking Pointers arnt supported in Java was making my brain slush, hehe, so i kept thinking &quot;does that mean no reference?&quot; <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top