MELJUN CORTES Java Lecture Threads

323 views

Published on

MELJUN CORTES Java Lecture Threads

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
323
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Java Lecture Threads

  1. 1. T hread s Concurrent Programming MELJUN CORTES MELJUNCORTES
  2. 2. What You Should Learn  What is a Thread?  Creating Threads  Using Threads  Synchronization
  3. 3. What are Threads?  Threads allow you to write concurrent operations in an OS-independent way.  Threads improve user experience. Single-user applications can return control to the user while performing a long-running operation.  Multi-user applications allow for multiple-users to use the system without have to wait for each other to finish. 
  4. 4. What are Threads?  Threads improve user experience. Single-user applications can return control to the user while performing a long-running operation.  Multi-user applications allow for multiple-users to use the system without have to wait for each other to finish. 
  5. 5. What are Threads?  Business applications rarely need to work directly with threads, but it’s useful to know what’s happening “under the covers”.
  6. 6. Creating Threads  There are two ways: Extend Thread  Implement Runnable 
  7. 7. Extend Thread  The quick-and-dirty way.  Override the run method: class Car extends Thread { public void run() { while(true) { System.out.println(“VROOM!”); } } }
  8. 8. Extend Thread  Using your Car class  Call the start method. Car car = new Car(); car.start();
  9. 9. Implementing Runnable  The more refined way.  Allows you to maintain a logical inheritance relationship.  The method you implement is also called run. class Car implements Runnable { public void run() { while(true) { System.out.println(“VROOM!”); } } }
  10. 10. Implementing Runnable  Using your Car class Feed your class to a Thread contructor.  Call the thread’s start method.  Car car = new Car(); Thread thread = new Thread(car); thread.start();
  11. 11. Threads  You can’t restart a dead thread.  Once the run() method has exited, the thread dies.
  12. 12. Thread.sleep()  Tells the thread to stop operating for at least the 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!”); } }
  13. 13. java.util.concurrent  Contains various utilities and structures for concurrent programming.  If you really need to implement serious multi-threading, use this instead of raw threads.  …but then again, are you planning to implement your own web server?
  14. 14. Synchronization  Most business applications are more concerned with synchronization than direct handling of threads.
  15. 15. Synchronization  When you synchronize on some code, a thread needs to obtain a “lock” on an object in order for to run the code.  Therefore, only one thread at a time will run the selected code.  We use the synchronized keyword.
  16. 16. Two Ways to Synchronize  On a method  On a block
  17. 17. Synchronizing on a Method  Synchronizing on a method means a thread will have a lock on the object being called.  No other thread can access the members of the object.
  18. 18. Synchronizing on a Method  Just place the synchronized keyword before the return type declaration of the method. public synchronized void method() { // some code here }
  19. 19. Synchronizing on a Block  You can specify which object the threads must lock on.  You can limit the synchronization to just part of the method.
  20. 20. Synchronizing 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
  21. 21. Synchronization Issues  Too much synchronization can cause bottlenecks if in a heavily multi-threaded applications.  Not enough synchronization can lead to bottlenecks.
  22. 22. Best Practice  Synchronize just enough for your needs.  Writing to resources in multi-threaded environments usually require synchronizing.  Reads should be synchronized if phantom-reads are a serious issue.
  23. 23. T he End Java Fundamentals and Object-Oriented Programming The Complete Java Boot Camp

×