Содержание
- 2. Concurrency A single application is often expected to do more than one thing at a time
- 3. Processes A process has a self-contained execution environment A process generally has a complete, private set
- 4. Threads I Threads are sometimes called lightweight processes Both processes and threads provide an execution environment,
- 5. Threads II Threads share the process's resources, including memory and open files From the application programmer's
- 6. Defining a Thread An application that creates an instance of Thread must provide the code that
- 7. Runnable Object The Runnable interface defines a single method, run, meant to contain the code executed
- 8. Runnable Object Example public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a
- 9. Runnable Object in Java 8 public static void main(String args[]) { Runnable r = () ->
- 10. Thread Subclass The Thread class itself implements Runnable, though its run method does nothing An application
- 11. Thread Subclass Example public class HelloThread extends Thread { public void run() { System.out.println("Hello from a
- 12. Runnable vs Thread Subclass A Runnable object employment is more general, because the Runnable object can
- 13. Pausing Execution with Sleep Thread.sleep causes the current thread to suspend execution for a specified period
- 14. Sleep Example public class SleepMessages { public static void main(String args[]) throws InterruptedException { String importantInfo[]
- 15. Thread Race Example Create two classes: first implements Runnable interface, and second extends Thread class. Method
- 16. Thread Race Example See 811ThreadRace project for the full text. * InfopulseTraining Center
- 17. Thread Terminations A thread terminates when: its run method returns, by executing a return statement after
- 18. Interrupted Status When the interrupt method is called on a thread, the interrupted status of the
- 19. How to Check Interrupted Status To find out whether the interrupted status was set, first call
- 20. InterruptedException If a thread is blocked, it cannot check the interrupted status This is where the
- 21. InterruptedException Example for (int i = 0; i // Pause for 4 seconds try { Thread.sleep(4000);
- 22. Joins The join method allows one thread to wait for the completion of another If t
- 23. Join Exercise Modify 811ThreadRace project so that first thread should wait for second thread finishing *
- 24. ThreadRace Class public static void main(String[] args) throws InterruptedException{ ThreadRunnab r = new ThreadRunnab(); Thread t1
- 25. Join Exercise See 812ThreadJoin project for the full text. * InfopulseTraining Center
- 26. Thread Priority public final void setPriority(int newPriority) - changes the priority of this thread public final
- 27. Sharing Resources Example Try to generate Fibonacci series in one thread and print its values in
- 28. Sharing Resources Example See 813Resources project for the full text. * InfopulseTraining Center
- 30. Скачать презентацию