Docsity

Java Threading: Understanding Thread Construction and Synchronization, Slides of Java Programming

Partial preview of the text

Download Java Threading: Understanding Thread Construction and Synchronization and more Slides Java Programming in PDF only on Docsity!

Multithreading

1 Introduction: definition• What is “Multi-threading?” 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 ordestroyed.

1 Introduction: priority• Since most computers have only one CPU, it is a myththat separate threads run simultaneously.• Because they are sharing the cycles of the one CPU, eachthread is given a

priority

—which is a number from 1-10.

( 5 is the default priority.)

1 Introduction: priority• Threads with a higher priority number are given moreturns at the CPU.• In fact, depending on the operating system, a thread’spriority can have a more drastic impact.

1 Introduction: preemption• When a thread of higher priority arrives andsteals 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 Windowsworld.• In a process known as

timeslicing

, each

1 Introduction: timeslicingthread is given a period of time to run, andthen all the other threads of equal priority gettheir turn at the CPU.• As with the preemption system, undertimeslicing, a higher-priority thread stillcommandeers the CPU immediately.

  • Class

Thread

has 7 different constructors.

2 Class

Thread

Methods: Constructors

  • When we wish to make a Java program usethreading, the easiest way is to say our class extends java.lang.Thread

the class

Thread

.

Thread()

—this constructor takes no arguments.

1.) Every instantiated thread object has a name.—When we use the no-argument Constructor, thenames are automatically generated.A thread created using the Constructor above wouldbe named: Thread-

,^ Thread-

,^ Thread-

, etc.

Thread(

String

name

)

—this Constructor takes a

String

argument,

which becomes the thread’s name.^ String

n

=

“cece”;

Thread

t

=

new

Thread(

n

);

t

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

t

=

new

Thread(

n

);

Thread

t

=

new

Thread();

t

would have the thread name of cece. t

would have the thread name of Thread-0.

public

class

MyThread

extends

Thread

As you would expect, webegin by extending the

class

Thread

.

public

class MyThread

extends

Thread

public MyThread(

String

n^ )

super(

n^ ); System.out.println(

“MyThread

Constructor”

First, add the constructormethod—which names thethread “Thread-

x ”.

Now, because it sub-classes Thread

, class

MyThread

is an instance of the threadclass.

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 samepriority as the thread that created it.