Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Stop a thread in java

static boolean stopRequested = false;
    public static synchronized boolean isStopped() {
        return stopRequested;
    }
    public static synchronized void stopRequested() {
        StopThread.stopRequested = true;
    }
    public static void main(String ar[]) throws InterruptedException {
        Thread runningThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!isStopped()) {
                    System.out.println(stopRequested);
                }
            }
        });
        runningThread.start();
        System.out.println("Stop Running Thread");
        stopRequested();
    }

No comments:

Post a Comment

Search 24 Bytes

Loading...