Java AdvancedLesson 79Free preview
Java Threads
This lesson is part of Java · All levels
Summary
Thread allows one program to perform multiple tasks simultaneously in one process. In Java, there are two main ways to create threads: inherit a thread class or implement a run-able interface. Multithreading speeds up heavy duty, but requires synchronization to avoid race condition.
Key points
- Start() running run() on the new thread; calling run() immediately stays on the caller thread
- Runnable is more recommended because classes can still inherit other classes
- Delay the thread for milliseconds, waiting for the other thread to finish
- Synchronized prevents two threads from executing critical blocks at the same time
- ExecutorService is more efficient at managing many tasks than making manual threads
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.