02b - ΣΔΥ50 - ΟΣΣ3 - Threads

download 02b - ΣΔΥ50 - ΟΣΣ3 - Threads

of 16

Transcript of 02b - ΣΔΥ50 - ΟΣΣ3 - Threads

  • 7/25/2019 02b - 50 - 3 - Threads

    1/16

    Threads

    3

  • 7/25/2019 02b - 50 - 3 - Threads

    2/16

    (process)

    ( )

  • 7/25/2019 02b - 50 - 3 - Threads

    3/16

    (thread) (, , )

    ( ) (tasks),

    ( )

    ,

    3

  • 7/25/2019 02b - 50 - 3 - Threads

    4/16

  • 7/25/2019 02b - 50 - 3 - Threads

    5/16

    :

    (process

    control block)

    (address space)

    :

    context-switching

    context-switching

  • 7/25/2019 02b - 50 - 3 - Threads

    6/16

    6

    Java :

    java.lang.Thread:

    ,

    java.lang.Runnable:

    run,

    run Runnable, Thread( Thread Runnable)

  • 7/25/2019 02b - 50 - 3 - Threads

    7/16

    Threadpublic class ExampleThread extends Thread

    {

    public void run()

    {// perform whatever thread needs to do

    }

    }

    threadExampleThread et = new ExampleThread();et.start();

    start() Thread

    Thread

  • 7/25/2019 02b - 50 - 3 - Threads

    8/16

    Runnableinterfacepublic class ExampleRunnable implements Runnable

    {

    public void run()

    {

    // perform whatever thread needs to do

    }

    }

    threadExampleRunnable er = new ExampleRunnable();

    new Thread(er).start();

    Runnable

  • 7/25/2019 02b - 50 - 3 - Threads

    9/16

    Ready to Run

    Running

    Dead

    Waiting

    Sleeping

    Blocked

    Non-runnable statesenteringnon-runnable

    leaving non-runnable

    terminates

    scheduling

    start()

    timesliceends

  • 7/25/2019 02b - 50 - 3 - Threads

    10/16

    10

  • 7/25/2019 02b - 50 - 3 - Threads

    11/16

    stack space,

    heap ( )

    :

    11

  • 7/25/2019 02b - 50 - 3 - Threads

    12/16

    ( ) monitor( semaphore)

    monitor

    monitor

    monitors ( mutex)

    Monitor:

  • 7/25/2019 02b - 50 - 3 - Threads

    13/16

    (race condition) ,

    ( )

    ,

    (race conditions)

  • 7/25/2019 02b - 50 - 3 - Threads

    14/16

    ,

    H Java ( ) synchronized

    (race conditions)

  • 7/25/2019 02b - 50 - 3 - Threads

    15/16

    15

    ( ) :

    public synchronized void xmethod {}

    xmethod (blocked)

    ( ):

    public int ymethod {

    ...

    synchronized (this) {

    ... }

    }

  • 7/25/2019 02b - 50 - 3 - Threads

    16/16

    16

    (Deadlocks)

    , (deadlock)

    Java