0Pricing
Java Academy · Lesson

Tasks & Lifecycles

Understand task states and lifecycles in concurrency: from creation to termination.

Tasks & Lifecycles is a free Java Academy lesson on CoddyKit — lesson 2 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Tasks represent units of work. Threads and executors run tasks, and each task goes through a lifecycle.

Thread states

Thread states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.

Code: lifecycle states

This shows thread states before and after execution.

public class Main {
  public static void main(String[] args) throws Exception {
    Thread t = new Thread(() -> System.out.println("Running task"));
    System.out.println("State before start: " + t.getState());
    t.start();
    t.join();
    System.out.println("State after finish: " + t.getState());
  }
}

Executor tasks

Executors manage tasks internally. Tasks are queued, picked up by threads, and then finished.

Code: executor lifecycle

This submits a task to an executor and shuts it down.

import java.util.concurrent.*;public class Main {
  public static void main(String[] args) throws Exception {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    Future<?> f = exec.submit(() -> System.out.println("Task in executor"));
    f.get();
    exec.shutdown();
    System.out.println("Executor shutdown called.");
  }
}

Waiting states

Threads can wait (WAITING) or wait with timeout (TIMED_WAITING). They become RUNNABLE again when notified.

Lifecycle check

Quick check: Which is NOT a common thread state?

Recap

Recap: Threads and tasks pass through states. Executors manage tasks and shutdown properly.

Frequently asked questions

Is the “Tasks & Lifecycles” lesson free?

Yes — the full text of “Tasks & Lifecycles” is free to read here on the web, and the Java Academy course includes 2 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Tasks & Lifecycles”?

Understand task states and lifecycles in concurrency: from creation to termination. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 2, so you can start here or from the beginning and move at your own pace.

How long does the “Tasks & Lifecycles” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Executors & Futures
  2. Tasks & Lifecycles
← Back to Java Academy