Solved

Threading

Posted on 2007-04-11
7
265 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 250 total points
Comment Utility
new Thread() {
    public void run() {
        // Do it
    }
}.start();
0
 
LVL 28

Author Comment

by:strickdd
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
strickdd,

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now