rams
rams

Reputation: 71

Continuous thread execution

Can any one help me? I want to execute a thread continuously (like infinite loop) in my project. I want to test the admin connections through the XRPC profile.

Thanks in advance.

Upvotes: 2

Views: 4849

Answers (3)

Bassem Reda Zohdy
Bassem Reda Zohdy

Reputation: 12942

Using Lambda and adding stop functionality:

    AtomicBoolean stop = new AtomicBoolean(false);
    Executors.newSingleThreadExecutor().execute(()->{
        while(!stop.get()){
            System.out.println("working");
        }
    });
    Thread.sleep(5);
    System.out.println("Stopping");
    stop.set(true);

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299068

The preferred Java 1.6 way to do this is the following:

Executors.newSingleThreadExecutor().execute(new Runnable(){
    @Override
    public void run(){
        while(true){
            // your code here
        }
    }
});

(Although it's almost equivalent to org.life.java's answer)

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240948

this will execute infinite [if no errors or exception occours]

new Thread(new Runnable(){public void run(while (true){/*your code*/})}).start();

Upvotes: 1

Related Questions