LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2006, 11:42 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
more java threads:


hello all,

i am having some real trouble with java threads. i want to be able to run this function in a thread, but keep it in the same class that i am using is that possible if so how.

the reason i want to run this in a thread is because as you can see it has a loop in it that will never end, and this consiquently will lock up the application if its not in a thread, however it has to be run in a thread otherwise it wont accept information passed from the server program


Code:
   public void getData() {            
            try {
                while( ( fromServer = in.readLine() ) != null ) {
                        // System.out.println( fromServer );
                        returnString = fromServer;
                        
                        if( fromServer.equals( "close." ) ) { break; }
                 }
            } catch( IOException errIO ) { }
    }

please please help.
 
Old 06-18-2006, 06:09 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
if you make your class implement the interface Runnable (java.lang) then you can write a method called run and you can then pass the same object to run in a new thread and it will run that method.
You will have something like this:

Code:
class YourClass implements Runnable {

  public void run() {
    getData();
  }
  
  ...

  public static void main(...) {
    YourClass yourCoolClass = new YourClass();
    Thread t = new Thread(yourCoolClass);
    t.start();
    // keep doing your things
  }
}
I believe I am not forgeting any steps, but I dont have anything to test it with

Hope this is useful and Hope I get your problem right!
 
Old 06-19-2006, 05:11 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
cheers for the reply, i managed to do something a long the lines of what you are saying this is the code for the working version:

Code:
     public void getData() {
         Thread t = new Thread();
         
        ( new Thread() {
            public void run() {
              try {
                  while( ( fromServer = in.readLine() ) != null ) {
                          System.out.println( fromServer );
                          returnString = fromServer;
                        
                          if( fromServer.equals( "close." ) ) { break; }
                   }
              } catch( IOException errIO ) { }
            }
        }).start();
     }
cheers for your help. trscookie.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads trouble (wait() and notify()) smoothdogg00 Programming 4 04-09-2006 12:05 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
java threads poeta_boy Programming 1 06-14-2005 04:34 PM
Java Threads - are they really redhatrosh Programming 1 04-05-2005 02:04 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration