Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Threading

Posted on 2007-04-11
7
Medium Priority
?
274 Views
Last Modified: 2011-09-20
I need to write a piece of code that will spawn a thread that will read in information from an inputstream. How can I create a thread that will execute specific code while I run other code in my main program?
0
Comment
Question by:strickdd
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 18892138
new Thread() {
    public void run() {
        // Do it
    }
}.start();
0
 
LVL 28

Author Comment

by:strickdd
ID: 18896973
So whatever code I want to do in the thread goes where "//Do it" is. I don't have to create a separate class or anything? Will the rest of the code  after this continue on once this thread is going? For example:

int temp = 0;

new Thread() {
    public void run() {
        // Do it
    }
}.start();

//will this execute?
temp = 1 * 2;
0
 
LVL 6

Assisted Solution

by:SamsonChung
SamsonChung earned 1000 total points
ID: 18900452
Strickdd, CEHJ's method is the quick and dirty way,
However, if you want, you can do the following

public class mythread extends Thread{
  //thread settings for your own thread
  boolean something
  public mythread(){
    //constructor of your own thread
  }
  public void run(){
    while (something){
      //read from System In, and other stuff
    }
  }
  public void synchronize stopme(){
    something = false;
  }
}

then you create this mythread as an object and start it.
then when you want to stop it, just call threadobject.stopme()
or add and extra while(true) Loop while sleep(1000) in the run method, so you can pause it, and restart it in your code.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 18902214
>>CEHJ's method is the quick and dirty way,

What makes you say that?

>>then when you want to stop it, just call threadobject.stopme()

You can only do that with something that's 'perpetual' anyway or it won't help you


0
 
LVL 28

Author Comment

by:strickdd
ID: 18904883
Thank you both for your solution. The reason I split the points is the Samson's was more complete and I ended up using his so I could manually stop the thread. CEHJ, your solution worked, but I was unable to use it with the lack of a stop method.

Thank you both for you help!
0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 18905438
CEHJ,

Just to let you know....
 quick and dirty way, IMHO, Fast, efficent, to the point, However, difficult to debug or learn. (quick and Dirty!!)
0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 18905440
strickdd,

Thanks, have fun in the mass world for Java Threads....
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question