OPERATING SYSTEM - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

OPERATING SYSTEM

Description:

Multithread : more than one threads can exist and run concurrently. ... There are two types of threads ... For the same priority threads there is no preemption. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 15
Provided by: saleku
Category:
Tags: operating | system

less

Transcript and Presenter's Notes

Title: OPERATING SYSTEM


1
OPERATING SYSTEM
  • TUTORIAL 3

http//www.cs.concordia.ca/k_karthi/comp346.html
2
Topics
  • Exception Handling contd
  • What is Thread
  • Thread Class, Scheduling
  • Thread Life Cycle
  • Useful Methods of Thread Class
  • Useful Methods of Object Class

3
Exception Handling contd.. Multiple catch
clauses class MultiCatch public static void
main(String args) try int a
args.length System.out.println(a
a) int b 42/a int c
1 c42 99 catch
(ArithmeticException e)
System.out.println(Divide by 0 e)
catch(ArrayIndexOutOfBoundsException e)
System.out.println(Array index oob e)
System.out.println(After try catch
blocks)
4
Thread
  • A thread is a single sequential flow of control
    within a program. A single thread has a
    beginning, a sequence, and an end . However, a
    thread itself is not a program it cannot run on
    its own. Rather, it runs within a program.
  • Multithread more than one threads can exist and
    run concurrently. They can perform different
    tasks in parallel. Multithreading is unique in
    Java.
  • Example when we download a video clip, we can
    playback the clip before the ending of download.
    One thread download it, another thread start
    playing.

5
Thread Scheduling
  • There are two types of threads
  • Green thread Java 1.2.1 and older versions use
    priority-based scheduling. If a higher-priority
    thread becomes ready, preemption occurs and
    processor is given to the higher-priority thread.
    So, the previously running thread must wait. For
    the same priority threads there is no preemption.
  • Native thread Scheduling depends on the native
    OS. This is not platform independent! But it
    supports multiple CPU.

6
Thread Class (1)
  • We can declare a class to be a subclass of
    Thread. This subclass should override the run
    method of class Thread.
  • For example, a thread that computes primes with
    in a range could be written as follows
  • class PrimeThread extends Thread
  • long minNum, maxNum
  • PrimeThread(long _minNum, long _maxNum)
  • // constructor
  • minNum _minNum maxNum _maxNum
  • public void run() //execution point
  • //compute primes in between minNum and maxNum

7
Thread Class (2)
  • An instance of this subclass can then be
    allocated and started.
  • The following code would then create a thread and
    start it running
  • PrimeThread p new PrimeThread(100, 200)
  • p.start()

8
Thread Life Cycle
9
Useful Methods of Thread Class (1)
  • start( ) Causes this thread to begin execution
    the Java Virtual Machine calls the run method of
    this thread. When we create an object of Thread
    Class it will not start execution.
  • run( ) This is the actual body of the Thread.
    User shall write necessary codes to do their task
    here.
  • Don't call a Thread's run method directly. If
    you do, you will not start a new thread you must
    use start which creates a new thread and then
    calls run for you.
  • Thread t new Thread( )
  • // Note we call t.start(), not t.run()
  • t.start()

10
Useful Methods of Thread Class (2)
  • yield( ) Causes the currently executing thread
    object to temporarily pause and allow other
    threads to execute. If we use priority-based
    scheduling, yield will allow the highest priority
    ready thread to run. In equal priority
    scheduling, a running thread will leave the CPU
    instantly by yield and according to Round Robin
    Scheduling algorithm next ready thread will run.
  • sleep( ) Causes the currently executing thread
    to sleep (temporarily cease execution) for the
    specified number of milliseconds.
  • join( ) Waits for this thread to die. It
    Throws InterruptedException - if another thread
    has interrupted the current thread

11
Some Useful Methods of Any Java Class
  • Class Object is the root of the class
    hierarchy. Every class has Object as a
    superclass. All objects, including arrays,
    implement the methods of this class.
  • wait( ) when a running thread calls wait the
    thread enters a waiting state for the particular
    object on which wait was called.
  • notify( ) Wakes up a single thread that is
    waiting on this object's monitor. If more than
    one threads are waiting on this object, one of
    them is chosen to be awakened. The choice is
    arbitrary.
  • notifyall( ) Wakes up all threads that are
    waiting on this object's monitor.

12
Synchronized block (1)
  • A synchronized block in java acts like a monitor.
  • class XYZ
  • public synchronized void method()
  • ...
  • if (...)
  • ...
  • wait()
  • ...
  • ...
  • No two threads can be executing inside of a
    synchronized block at the same time
  • A synchronized block maintains a queue of FCFS to
    have access inside
  • If a thread waits inside a synchronized block
    other thread can enter

13
Java Threads "synchronized" Keyword
contd.. The primary tool for managing
coordination between threads in Java programs is
the "synchronized" keyword. To make your
programs thread-safe, you must identify what
data will be shared across threads. If you are
writing data that may be read later by another
thread and vice versa, then that data is shared,
and you must synchronize when accessing
it. http//java.sun.com/docs/books/tutorial/essen
tial/threads/multithreaded.html
14
References
  • Deitel Deitel, JAVA How to Program Prentice
    Hall
  • The JAVA Tutorial
  • http//java.sun.com/docs/books/tutorial/
  • http//java.sun.com/products/jdk/1.2/docs/api/java
    /lang/Thread.html
Write a Comment
User Comments (0)
About PowerShow.com