Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main implements Runnable {
- private boolean running = true;
- public void stopExecution ()
- {
- running = false;
- }
- public void run() {
- do
- {
- System.out.println("Hello from a thread!");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {}
- } while( running );
- }
- public static void main(String args[]) {
- Main main = new Main();
- Thread thread = new Thread( main );
- thread.start();
- //Call stopExecution(); at some point, e.g. key release
- try {
- Thread.sleep(2500);
- } catch (InterruptedException e) {}
- main.stopExecution();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement