Use ScheduledExecutorService to manage thread - Java Thread

Java examples for Thread:ScheduledExecutorService

Description

Use ScheduledExecutorService to manage thread

Demo Code



import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestScheduledExecutor implements Runnable {
    private int i;

    public static void main(String[] args) {
        //w w  w  .jav a 2  s.co  m
        ScheduledExecutorService e = Executors
                .newSingleThreadScheduledExecutor();
        TestScheduledExecutor t = new TestScheduledExecutor();

        e.scheduleAtFixedRate(t, 0, 7, TimeUnit.SECONDS);

    }

    public static void printTask(int a) {
        System.out.println(a);
    }

    @Override
    public void run() {
        
        i = i + 1;
        printTask(i);
    }

}

Related Tutorials