Accessing a thread

Discussion in 'Java' started by Dirk Bruere at NeoPax, Jul 30, 2012.

  1. From: 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?




    --
    Dirk

    Full Spectrum Praxis : ZERO STATE : http://zerostate.net

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Dirk Bruere at NeoPax, Jul 30, 2012
    #1
    1. Advertising

  2. Dirk Bruere at NeoPax

    Eric Sosman Guest

    To: Dirk Bruere at NeoPax
    From: Eric Sosman <>

    On 7/30/2012 5:54 AM, Dirk Bruere at NeoPax wrote:
    > 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.

    --
    Eric Sosman
    d

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Eric Sosman, Jul 30, 2012
    #2
    1. Advertising

  3. On 30/07/2012 20:00, Eric Sosman wrote:
    > To: Dirk Bruere at NeoPax
    > From: Eric Sosman<>
    >
    > On 7/30/2012 5:54 AM, Dirk Bruere at NeoPax wrote:
    >> 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.
    >
    > --
    > Eric Sosman
    > d
    >
    > --- BBBS/Li6 v4.10 Dada-1
    > * Origin: Prism bbs (1:261/38)
    > --- Synchronet 3.16a-Win32 NewsLink 1.98
    > Time Warp of the Future BBS - telnet://time.synchro.net:24


    Thanks. It's obvious now!

    --
    Dirk

    Full Spectrum Praxis : ZERO STATE : http://zerostate.net
    Dirk Bruere at NeoPax, Jul 30, 2012
    #3
  4. Dirk Bruere at NeoPax

    Roedy Green Guest

    To: Dirk Bruere at NeoPax
    From: Roedy Green <>

    On Mon, 30 Jul 2012 10:54:25 +0100, Dirk Bruere at NeoPax
    <> wrote, quoted or indirectly quoted someone who
    said :

    >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)
    http://www.youtube.com/watch?v=F-QA2rkpBSY

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
    Roedy Green, Jul 31, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,532
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    473
  3. Stephen Miller
    Replies:
    3
    Views:
    3,950
    Stephen Miller
    Jul 2, 2004
  4. Johanna
    Replies:
    0
    Views:
    595
    Johanna
    Oct 13, 2004
  5. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,943
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
Loading...

Share This Page