Solved

Threading

Posted on 2007-04-11
7
270 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
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!

 
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

Technology Partners: 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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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