java programming language_8_multithread concurrency

Upload: mhoa43

Post on 02-Jun-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    1/23

    NGN NG LP TRNHJAVA

    1

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    2/23

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    3/23

    MULTITHREADS &

    CONCURRENCY

    3

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    4/23

    Nidung

    1. Giithiu

    2. Tothreads

    3. Unresponsive User Interface

    4

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    5/23

    1. Giithiuvalung

    nlung(single thread) :Mtimvo (entry point) : main() method

    Mtimra (exit point)

    alung(multi threads):Mtimvo ban u(main() method)

    Theo sau binhiuimvo, imra chysong song vimain()

    5

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    6/23

    1.1anhim(multi-tasking)Hthnganhim

    Vd: Hiuhnh Windows,Unix Thchinnhiutc vngthibngcch chia sti nguyn : CPU,

    bnh, cc knh vo ra (I/O channels) Single-CPU machine : mttc vtimtthiim Multi-CPU machine: nhiutc vc ththchintimtthiim

    Mcch: Gip tiuho vicsdngti nguyn

    Phnloi Co-operative multitasking systems:

    Mitc vphitnguynaliquyniukhincho tc vkhc Stin cy/tin tnggiacc tc v Mttc vkhng hptc c thtreo hthng

    Pre-emptive multitasking systems: Cc tc vchytrong mtkhung thigian xc nh Htkhung thigian, phialiquyniukhincho cc tc vkhc

    6

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    7/23

    1.2alung(multi-threading in aprocess) Khi nimprocess:

    /v Unix, Linux: tora mtprocess mi

    /v Windows: chymtchngtrnh

    cimcaprocess: C khng gian achring

    C khiiukhinring

    Sdngnhiuti nguyn hthng=> heavyweight

    Trong mtprocess, c thchynhiuthreads cithinhiu

    nnghthng

    7

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    8/23

    1.2alung

    cimcathread L mtlightweight process

    Chybn trong mtprocess

    Cc threads chia s

    cng mtkhng gian ach,

    Cng mttpcc ti nguyn cphn phi

    V mi trngcaprocess

    Phitqunl ti nguyn bn trong mtprocess : Stack, registers, program counter

    8

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    9/23

    1.2alung

    Lich cavicsdngmultithreading trong mtchngtrnh

    tnghiusut= tiuvicsdngti nguyn Vd: mtthread angbblock (chmtthao tc I/O ktthc), thread

    khc c thsdngCPU thchinvictnh ton => hiunngvthng lngcachngtrnh stthn

    mbostngtc tthnvingidng Vd: trong mtngdngword, trong khi mtthread angin hoclutr

    file, mtthread khc c thcsdngtiptcsontho

    Trong ngdngGUI, multit-threading mbocung cpmtresponsive user interface

    ngdngSwing datrn multi-threading => c thsdngminhhochh/ camulti-threading

    9

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    10/23

    1.3 Java profiling

    Java profiling l g? Application Developer:Monitor, profile, take thread dumps,

    browse heap dumps

    System Administrator:Monitor and control Java applicationsacross the entire network

    Java Application User:Create bug reports containing all thenecessary information

    Phnmm: YOURKIT

    http://www.yourkit.com/ Commercial tool

    VisualVM

    http://visualvm.java.net/

    free

    10

    http://www.yourkit.com/http://visualvm.java.net/http://visualvm.java.net/http://visualvm.java.net/http://www.yourkit.com/http://www.yourkit.com/http://www.yourkit.com/
  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    11/23

    VisualVM

    11

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    12/23

    VisualVM

    Ci t Visual VM:

    https://java.net/projects/visualvm/downloads/download/release137/visualvm_137.zip

    Eclipse plugin:http://java.net/projects/visualvm/downloads/download/release133/visualvm_lau

    ncher_u1_eclipse_36.zip Unzip the archive into directory of your choice

    Add local update site pointing to the chosen directory

    Install VisualVM Launcher Feature

    Cuhnh Go to Run/Debug-Launching-VisualVM Configurationpreferences :

    JDK Home to be used by VisualVM (needs to be a JDK, not JRE) The path to VisualVM executable (VisualVM 1.1+ is supported)

    Sdng Create a new application configuration

    Choose the "VisualVM Launcher"as the application launcher

    Run/Debug the application with the created configuration to have VisualVMstarted automatically

    12

    https://java.net/projects/visualvm/downloads/download/release137/visualvm_137.ziphttps://java.net/projects/visualvm/downloads/download/release137/visualvm_137.ziphttp://java.net/projects/visualvm/downloads/download/release133/visualvm_launcher_u1_eclipse_36.ziphttp://java.net/projects/visualvm/downloads/download/release133/visualvm_launcher_u1_eclipse_36.ziphttp://visualvm.java.net/images/eclipse/preferences.pnghttp://visualvm.java.net/images/eclipse/configuration.pnghttp://visualvm.java.net/images/eclipse/launcher.pnghttp://visualvm.java.net/images/eclipse/launcher.pnghttp://visualvm.java.net/images/eclipse/configuration.pnghttp://visualvm.java.net/images/eclipse/preferences.pnghttp://java.net/projects/visualvm/downloads/download/release133/visualvm_launcher_u1_eclipse_36.ziphttp://java.net/projects/visualvm/downloads/download/release133/visualvm_launcher_u1_eclipse_36.ziphttp://java.net/projects/visualvm/downloads/download/release133/visualvm_launcher_u1_eclipse_36.ziphttp://java.net/projects/visualvm/downloads/download/release133/visualvm_launcher_u1_eclipse_36.ziphttps://java.net/projects/visualvm/downloads/download/release137/visualvm_137.ziphttps://java.net/projects/visualvm/downloads/download/release137/visualvm_137.ziphttps://java.net/projects/visualvm/downloads/download/release137/visualvm_137.zip
  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    13/23

    2. Threads

    Class threads

    13

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    14/23

    2. Threads

    Cch tothreads:

    Extend a subclass from the super class Thread and

    override the run() method to specify the running behavior

    of the thread.

    Create an instance and invoke the start() method, which

    will call-back the run() on a new thread.

    14

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    15/23

    VD

    15

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    16/23

    3. Unresponsive User Interface

    UUI cm tbimtngdngSWING vimtvnglpm

    C 2 buttons: Start Counting v Stop Counting

    2 handlers ca2 buttons giao tipthng qua mtbinboolean stop The stop-button handler sets the stop flag;

    while the start-button handler checks if stop flag has been set

    before continuing the next count.

    16

    17

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    17/23

    Vd1: UnresponsiveUI (1/2)

    17

    18

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    18/23

    Vd1: UnresponsiveUI (2/2)

    18

    19

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    19/23

    Tracing the threads

    1. The main() method starts in the "main" thread.

    2. A new thread "AWT-Windows" (Daemon thread) is startedwhen we step-into the constructor "new UnresponsiveUI()"(because of the "extends JFrame").

    3. After executing "setVisible(true)", another two threads arecreated - "AWT-Shutdown" and "AWT-EventQueue-0" (i.e.,the EDT).

    4. The "main" thread exits after the main() method completes. Anew thread called "DestroyJavaVM" is created.

    5. At this point, there are 4 threads running - "AWT-Windows","AWT-Shutdown" and "AWT-EventQueue-0 (EDT)" and"DestroyJavaVM".

    6. Clicking the START button invokes the actionPerformed() inthe EDT.

    19

    20

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    20/23

    Vd2: UnresponsiveUI with Thread

    20

    21

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    21/23

    Tracing the threads

    When the "START" button is clicked, a new thread called

    "Thread-n" (n is a running number) is created to run the

    compute-intensive counting-loop.

    However, this thread is not programmed to yield control to

    other threads, in particular, the event-dispatching thread.

    This program is, however, slightly better than the previousprogram.

    The display is updated, and the clicking of "STOP" button

    has its effect after some delays.

    21

    22

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    22/23

    Vd3: Unresponsive UI with sleep

    Let's modify the program by making a call to the counting-

    thread's sleep()method,

    which requests the counting-thread to yield control to the

    event-dispatching thread to update the display and

    response to the "STOP" button.

    The counting program now works as desired.

    The sleep() method also provides the necessary delay

    needed.

    22

    23

  • 8/11/2019 Java Programming Language_8_Multithread Concurrency

    23/23

    23