Solved

Threading

Posted on 2007-04-11
7
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 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 250 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

691 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