Code Monkey home page Code Monkey logo

java-mod-5-managing-threads's Introduction

Managing Threads

Learning Goals

  • Use different methods to control threads.

Introduction

We’ll look at some of the common methods used for managing threads in Java. After you start a thread, it’s sometimes necessary to control their state.

Sleeping Threads

The Thread.sleep method causes the currently executing thread to suspend its execution for a specified amount of time (usually in milliseconds). This frees up CPU time for other threads that are running in the process. Let’s look at an example.

class Example {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            System.out.println("hello from the thread!");
        });

        t.start();
        System.out.println("started");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("done");
    }
}
started
Hello from the thread!
done // printed after at least 1000 milliseconds

The Thread.sleep method throws the checked exception InterruptedException. In this code, we are telling the thread to be suspended for 1000 milliseconds. This time is the minimum amount of time the thread will be suspended. Sometimes the thread may be suspended for longer than 1000 milliseconds.

Joining Threads

The join instance method on threads causes the current thread to wait for completion of its execution before executing another thread. If we run the following code without the t.join() statement, the “done” string will be printed before the t thread finishes execution.

class Example {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                System.out.println("hello from the thread!");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        });

        System.out.println("start");
        t.start();
//        t.join();
        System.out.println("done");
    }
}
start
done
hello from the thread!

Let’s run the code again with the t.join statement.

class Example {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                System.out.println("hello from the thread!");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        });

        System.out.println("start");
        t.start();
        t.join();
        System.out.println("done");
    }
}
start
hello from the thread!
done

This time the program waits for the t thread to complete its execution before continuing execution of the main thread.

The join method can also take a time argument (milliseconds) which defines the amount of time to wait before moving on to another thread. This prevents the thread from running indefinitely.

class Example {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("hello from the thread!");
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        });

        System.out.println("start");
        t.start();
        t.join(1000);
        System.out.println("done");
    }
}
start
done
hello from the thread!

In this case, the t thread is executing for 3000 milliseconds while the join method is given an argument of 1000 milliseconds. The execution of the t thread is paused after 1000 milliseconds and the main thread is executed instead. Then the t thread is given time on the CPU again to complete execution.

Conclusion

We’ve learned how to suspend currently executing threads and how to wait for the current thread to execute before running other threads. We’ll learn how to use these methods to write safe, concurrent code in the later lessons.

java-mod-5-managing-threads's People

Contributors

alveem avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.