- package javaapplication1;
- public class Main
- {
- public static void main(String[] args) throws Exception
- {
- Runner mRunner = new Runner();
- // Allocates a new Thread object.
- // mRunner - the object whose run method is called.
- // start() - causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
- new Thread(mRunner).start();
- for (int i = 0; i < 1000; i++)
- {
- System.out.println(" ------" + Thread.currentThread());
- }
- // Set message to stop the thread.
- mRunner.makeStop();
- }
- }
- // The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.
- class Runner implements Runnable
- {
- boolean makeStop = false;
- @Override
- public void run()
- {
- while (true)
- {
- System.out.println("++++++ " + Thread.currentThread());
- if (makeStop == true)
- {
- return;
- }
- }
- }
- public void makeStop()
- {
- this.makeStop = true;
- }
- }