Download - MELJUN CORTES Java Lecture Threads
ThreadsThreadsConcurrent ProgrammingConcurrent Programming
MELJUNCORTEMELJUNCORTESS
MELJUN CORTESMELJUN CORTES
What You Should LearnWhat You Should Learn
What is a Thread?What is a Thread? Creating ThreadsCreating Threads Using ThreadsUsing Threads SynchronizationSynchronization
What are Threads?What are Threads?
Threads allow you to write concurrent Threads allow you to write concurrent operations in an OS-independent way.operations in an OS-independent way.
Threads improve user experience.Threads improve user experience. Single-user applications can return control to Single-user applications can return control to
the user while performing a long-running the user while performing a long-running operation.operation.
Multi-user applications allow for multiple-users Multi-user applications allow for multiple-users to use the system without have to wait for to use the system without have to wait for each other to finish.each other to finish.
What are Threads?What are Threads?
Threads improve user experience.Threads improve user experience. Single-user applications can return control to Single-user applications can return control to
the user while performing a long-running the user while performing a long-running operation.operation.
Multi-user applications allow for multiple-users Multi-user applications allow for multiple-users to use the system without have to wait for to use the system without have to wait for each other to finish.each other to finish.
What are Threads?What are Threads?
Business applications rarely need to work Business applications rarely need to work directly with threads, but it’s useful to directly with threads, but it’s useful to know what’s happening “under the know what’s happening “under the covers”.covers”.
Creating ThreadsCreating Threads
There are two ways:There are two ways: Extend ThreadExtend Thread Implement RunnableImplement Runnable
Extend ThreadExtend Thread
The quick-and-dirty way.The quick-and-dirty way.
Override the Override the runrun method: method:
class Car extends Thread { public void run() { while(true) { System.out.println(“VROOM!”); } }}
Extend ThreadExtend Thread
Using your Car classUsing your Car class Call the Call the startstart method. method.
Car car = new Car(); car.start();
Implementing RunnableImplementing Runnable The more refined way.The more refined way.
Allows you to maintain a logical inheritance Allows you to maintain a logical inheritance relationship.relationship.
The method you implement is also called The method you implement is also called runrun..
class Car implements Runnable { public void run() { while(true) { System.out.println(“VROOM!”); } }}
Implementing RunnableImplementing Runnable
Using your Car classUsing your Car class Feed your class to a Thread contructor.Feed your class to a Thread contructor. Call the thread’s start method.Call the thread’s start method.
Car car = new Car(); Thread thread = new Thread(car); thread.start();
ThreadsThreads
You can’t restart a dead thread.You can’t restart a dead thread. Once the run() method has exited, the thread Once the run() method has exited, the thread
dies.dies.
Thread.sleep()Thread.sleep()
Tells the thread to stop operating for at least the Tells the thread to stop operating for at least the number of milliseconds of the parameter.number of milliseconds of the parameter.
public void run() { while(true) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(“VROOM!”); } }
java.util.concurrentjava.util.concurrent
Contains various utilities and structures Contains various utilities and structures for concurrent programming.for concurrent programming.
If you really need to implement serious If you really need to implement serious multi-threading, use this instead of raw multi-threading, use this instead of raw threads.threads.
……but then again, are you planning to but then again, are you planning to implement your own web server?implement your own web server?
SynchronizationSynchronization
Most business applications are more Most business applications are more concerned with synchronization than concerned with synchronization than direct handling of threads.direct handling of threads.
SynchronizationSynchronization
When you synchronize on some code, a When you synchronize on some code, a thread needs to obtain a “lock” on an thread needs to obtain a “lock” on an object in order for to run the code.object in order for to run the code.
Therefore, only one thread at a time will Therefore, only one thread at a time will run the selected code.run the selected code.
We use the We use the synchronizedsynchronized keyword. keyword.
Two Ways to SynchronizeTwo Ways to Synchronize
On a methodOn a method On a blockOn a block
Synchronizing on a MethodSynchronizing on a Method
Synchronizing on a method means a Synchronizing on a method means a thread will have a lock on the object being thread will have a lock on the object being called.called.
No other thread can access the members No other thread can access the members of the object.of the object.
Synchronizing on a MethodSynchronizing on a Method
Just place the Just place the synchronizedsynchronized keyword keyword before the return type declaration of the before the return type declaration of the method.method.
public synchronized void method() { // some code here}
Synchronizing on a BlockSynchronizing on a Block
You can specify which object the threads You can specify which object the threads must lock on.must lock on.
You can limit the synchronization to just You can limit the synchronization to just part of the method.part of the method.
Synchronizing on a BlockSynchronizing on a Block
List list = new ArrayList();... // in the middle of a method synchronized(list) { list.add(“bla bla”);}... // in the middle of a method
Synchronization IssuesSynchronization Issues
Too much synchronization can cause Too much synchronization can cause bottlenecks if in a heavily multi-threaded bottlenecks if in a heavily multi-threaded applications.applications.
Not enough synchronization can lead to Not enough synchronization can lead to bottlenecks.bottlenecks.
Best PracticeBest Practice
Synchronize just enough for your needs.Synchronize just enough for your needs.
Writing to resources in multi-threaded Writing to resources in multi-threaded environments usually require environments usually require synchronizing.synchronizing.
Reads should be synchronized if Reads should be synchronized if phantom-reads are a serious issue.phantom-reads are a serious issue.
The EndThe End
Java Fundamentals and Object-Oriented Java Fundamentals and Object-Oriented ProgrammingProgramming
The Complete Java Boot CampThe Complete Java Boot Camp