Accessing a thread

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

File1

public class controller extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final LanSendThread lanSendThread = new LanSendThread();
lanSendThread.start();
....}

__________

File2

public class LanSendThread extends Thread{
public static Handler lanSendHandler;

@Override
public void run(){
Looper.prepare();

lanSendHandler = new Handler() {
//stuff}

___________

How do I access the thread lanSendThread from another class in another file?
 
E

Eric Sosman

File1

public class controller extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final LanSendThread lanSendThread = new LanSendThread();
lanSendThread.start();
...}

__________

File2

public class LanSendThread extends Thread{
public static Handler lanSendHandler;

@Override
public void run(){
Looper.prepare();

lanSendHandler = new Handler() {
//stuff}

___________

How do I access the thread lanSendThread from another class in another
file?

The same way you'd access it if it were an ArrayList or
a JButton or a File: You save the reference somewhere and dish
it out to interested parties. There must be two or three jillion
ways to do this; a few of them are

- Make `lanSendThread' a public member of the controller
class (poor choice of name, by the way). You may or may
not want to make that member `final'.

- Make `lanSendThread' a private member of the controller
class, and write a public getThread() method to return it.

- Stash the value of `lanSendThread' in a Map or other data
structure, and "publicize" the data structure and/or
accessors for it.
 
R

Roedy Green

How do I access the thread lanSendThread from another class in another file?


// execute InParallel.run()
// in parallel to this thread on a new thread.
Thread t = new Thread ( new InParallel() );

// Note we call t.start(), not t.run()
// t.run() would just call run in the ordinary way.

// Get reference to the thread running this
// code right now.
Thread runningNow = Thread.currentThread();

Keep in mind a thread is busy running its own code. Pretty well
anything you run on that thread is going to be highly disruptive.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top