Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Multithreading-Thread Programming-Lecture Slides, Slides of Computer Engineering and Programming

This lecture was delivered by Aniruddh Parmar at B. R. Ambedkar Bihar University for Data Transfer Programming course. It includes: Multithreading, Program, Execution, Simultaneous, Concurrent, Priority, Sharing, Default, Preemption, Timeslicing

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(4)

39 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Multithreading
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Multithreading-Thread Programming-Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Multithreading

  • What is “Multi-threading?”

1 Introduction: definition

thread

The basic unit of program execution.

A process can have several threads running concurrently,

each performing a different job.

When a thread has finished its job, it is suspended or

destroyed.

  • Since most computers have only one CPU, it is a myth

that separate threads run simultaneously.

1 Introduction: priority

  • Because they are sharing the cycles of the one CPU, each

thread is given a priority —which is a number from 1-10.

( 5 is the default priority.)

1 Introduction: priority

  • In fact, depending on the operating system, a thread’s

priority can have a more drastic impact.

  • Threads with a higher priority number are given more

turns at the CPU.

1 Introduction: preemption

  • When a thread of higher priority arrives and

steals the processor from a lower-priority thread,

this is known as preemption.

  • One weakness of the preemption system is this:

if another thread of the same priority is waiting

to run, it is shut out of the processor.

This is called thread starvation.

  • Another system is used in the Windows

world.

  • In a process known as timeslicing , each

thread is given a period of time to run, and

then all the other threads of equal priority get

their turn at the CPU.

1 Introduction: timeslicing

  • As with the preemption system, under

timeslicing, a higher-priority thread still

commandeers the CPU immediately.

  • Class Thread has 7 different constructors.

2 Class Thread Methods: Constructors

java.lang.Thread
  • When we wish to make a Java program use

threading, the easiest way is to say our class

extends the class Thread.

Thread()—this constructor takes no arguments.

1.) Every instantiated thread object has a name.

—When we use the no-argument Constructor, the

names are automatically generated.

A thread created using the Constructor above would

be named:

Thread-0, Thread-1, Thread-2, etc.

Thread( String name )

—this Constructor takes a String argument,

which becomes the thread’s name.

String n = “cece”;

Thread t2 = new Thread( n );

t2 would have the thread name of cece.

Thread( String name )

—this Constructor takes a String argument,

which becomes the thread’s name.

String n = “cece”;

Thread t3 = new Thread( n );

Thread t4 = new Thread();

t3 would have the thread name of cece.

t4 would have the thread name of Thread-0.

public class MyThread extends Thread {

}

As you would expect, we
begin by extending the
class Thread.

public class MyThread extends Thread { public MyThread( String n ) { super( n ); System.out.println( “MyThread Constructor” ); }

}

First, add the constructor
method—which names the
thread “Thread- x ”.
Now, because it sub-classes
Thread, class MyThread
is an instance of the thread
class. Class^ Thread^ contains a
method run() that is
empty—it does nothing.
So, if you want anything to
happen in your thread, you
need to override the method
run().

public void run() { System.out.println( “In run! \ n” ); }

public class MyThread extends Thread { public MyThread( String n ) { super( n ); System.out.println( “MyThread Constructor” ); } public void run() { System.out.println( “In run! \ n” ); }

public class TestMyThread { public static void main( String[] args ) {

} }

MyThread chuck = new MyThread( "Charlie" );

So, will we already be running a
thread in main now?

MyThread dede = new MyThread ( ”Deirdra" );

MyThread pat = new MyThread ( “Patrick" );

chuck.start();

dede.start();

pat.start();

main() will instantiate and start() all these threads. After they have
started, the methods immediately return and the thread main() dies.
However the entire application doesn’t die until the last thread does.

Docsity.com

public class MyThread extends Thread { public MyThread( String n ) { super( n ); System.out.println( “ In Constructor ” ); } public void run() { System.out.println( “ In run!\ n“ ); } } public class TestMyThread { public static void main( String[] args ) { MyThread chuck = new MyThread( “Charles” ); chuck.start(); MyThread dede = new MyThread( “Deirdra” ); dede.start(); MyThread pat = new MyThread( “Patrick” ); pat.start(); System.out.println( "chuck=" + chuck.toString() ); System.out.println( " dede=" + dede.toString() ); System.out.println( " pat=" + pat.toString() ); } }

chuck=Thread[Charlie,5,main] dede=Thread[Deirdra,5,main] pat=Thread[Patrick,5,main]

•Thread name
  • priority
  • thread groups
By default, a new thread has the same
priority as the thread that created it.