meljun cortes java lecture threads
DESCRIPTION
MELJUN CORTES Java Lecture ThreadsTRANSCRIPT
![Page 1: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/1.jpg)
ThreadsThreadsConcurrent ProgrammingConcurrent Programming
MELJUNCORTEMELJUNCORTESS
MELJUN CORTESMELJUN CORTES
![Page 2: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/2.jpg)
What You Should LearnWhat You Should Learn
What is a Thread?What is a Thread? Creating ThreadsCreating Threads Using ThreadsUsing Threads SynchronizationSynchronization
![Page 3: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/3.jpg)
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.
![Page 4: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/4.jpg)
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.
![Page 5: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/5.jpg)
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”.
![Page 6: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/6.jpg)
Creating ThreadsCreating Threads
There are two ways:There are two ways: Extend ThreadExtend Thread Implement RunnableImplement Runnable
![Page 7: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/7.jpg)
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!”); } }}
![Page 8: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/8.jpg)
Extend ThreadExtend Thread
Using your Car classUsing your Car class Call the Call the startstart method. method.
Car car = new Car(); car.start();
![Page 9: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/9.jpg)
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!”); } }}
![Page 10: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/10.jpg)
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();
![Page 11: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/11.jpg)
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.
![Page 12: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/12.jpg)
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!”); } }
![Page 13: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/13.jpg)
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?
![Page 14: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/14.jpg)
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.
![Page 15: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/15.jpg)
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.
![Page 16: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/16.jpg)
Two Ways to SynchronizeTwo Ways to Synchronize
On a methodOn a method On a blockOn a block
![Page 17: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/17.jpg)
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.
![Page 18: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/18.jpg)
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}
![Page 19: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/19.jpg)
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.
![Page 20: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/20.jpg)
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
![Page 21: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/21.jpg)
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.
![Page 22: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/22.jpg)
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.
![Page 23: MELJUN CORTES Java Lecture Threads](https://reader033.vdocuments.mx/reader033/viewer/2022052823/5552e8d2b4c90532498b5538/html5/thumbnails/23.jpg)
The EndThe End
Java Fundamentals and Object-Oriented Java Fundamentals and Object-Oriented ProgrammingProgramming
The Complete Java Boot CampThe Complete Java Boot Camp