Forum Stats

  • 3,827,144 Users
  • 2,260,745 Discussions
  • 7,897,182 Comments

Discussions

How do I implement a Timed thread in Java 8?

3192389
3192389 Member Posts: 2
edited Apr 13, 2016 2:18AM in Java 8 Questions

Hello,

I hope this is the correct forum.  (Java 8?)

Anyway, how would I start to implement this code with Java 8:

    public static void main(String[] args) {
                   
        Wait wait = new Wait(10000); //wait for ten seconds

        try
        {
            wait.StartWait(); //return immediately
            Thread.sleep(11000);
            wait.StopWait();
        }
        catch (Exception ex)
        {
            System.out.print("Timer timed out.");
        }

        System.out.print("Success!");       
    }

Again, I need to return immediately from the StartWait method.

Thank you.

williamj

Best Answer

  • Arpit1992
    Arpit1992 Member Posts: 10
    edited Apr 13, 2016 2:18AM Answer ✓

    In order to create a thread in java you instantiate an Object of - java.lang.Thread class.

    Now in the constructor of that object you need to provide -Runnable Type.

    Runnable is an interface in Java. Go and implement it in your class and override the public void run(){} ..

    That it Folks!!!

Answers