?
Solved

Threading

Posted on 2007-04-11
7
Medium Priority
?
272 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 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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