Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Java Concurrency Lab Extend Thread Java Concurrency Lab Extend Thread Presenter Name Agenda 9/3/20XX What is the Thread Class Extending the Thread Class Simple Example Presentation...

1 answer below »
Java Concu
ency Lab Extend Thread
Java Concu
ency
Lab Extend Thread
Presenter Name
Agenda
9/3/20XX
What is the Thread Class
Extending the Thread Class Simple Example
Presentation Title
2
Introduction
A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.
Presentation Title
3
Introduction
A single java process can have many threads.
The main process can spawn thread, or a thread can spawn a new thread!
Presentation Title
4
Introduction
We can define a thread in the following two ways:
By extending Thread class
By implementing Runnable interface
5
Thread
Thread
(Class)
Runnable
(Interface)
un()
method
Ove
ide
Extends
Implements
When to extending the thread class
In General, you would rarely need to do this.
Extending the Thread class is reserved for creating new thread functionality.
If I need to change how a thread does something which given the nature of this, it would be fraught with danger.
Presentation Title
6
Thread Class
Thread a line of execution within a program.
Each program can have multiple associated threads.
Each thread has a priority which is used by the thread scheduler to determine which thread must run first.
Java provides a thread class that has various method calls in order to manage the behavior of threads by providing constructors and methods to perform operations on threads.
Presentation Title
7
Thread.sleep()
Causes the cu
ently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
The thread does not lose ownership of any monitors.(Don’t wo
y about this yet)
8
join()
The join() method which allows one thread to wait until another thread completes its execution.
If t is a Thread object whose thread is cu
ently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program.
9
Thread 1
Thread 2
Main Thread
Run
Run
Join
Join
Implement Runnable Interface
Simple Example
Note
The method in the interface is run.
By being runnable a class can be used to create a thread.
10
Here we instantiate Several PrintWord objects.
Each one is used to create a thread.
A
ayList threads = new A
ayList();
Create a print word (which is runnable)
PrintWord p = new PrintWord("First", 5);
Create a thread
Thread t = new Thread(p);
threads.add(t);
Create a print word (which is runnable)
PrintWord p2 = new PrintWord("Second", 5);
Create a thread
Thread t2 = new Thread(p2);
threads.add(t2);
Create a print word (which is runnable)
PrintWord p3 = new PrintWord("Third", 5);
Create a thread
Thread t3 = new Thread(p3);
threads.add(t3);
Now we start the threads
for( Thread thread : threads)
{
    thread.run();
}
If we need the thread to combine information when they are done –
Join() will wait until the thread ends before continuing
for( Thread thread : threads)
{
try {
    thread.join();
} catch (Inte
uptedException e) {
    e.printStackTrace();
};
}
Example Output:
Not the thread complete so quickly it does not have time to switch
Exercise
We will compare a single version and multi thread version of the following process:(We will create the a
ays one at a time - think about why)   
Create the a
ays of the specified size
Load each slot of the a
ay with a random number (0 – 999)     
In a separate step get the sum of all number in the a
ay
Exercise
We will create two methods that do the previous process
One Will be single Thread
A second Will be Multiple Threads
We will compare the time it takes each
This will vary on different machines
We will do this as a group
Exercise
Timings:
long startTime = System.cu
entTimeMillis();
Do work here (call method that does task)
long endTime = System.cu
entTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime) + "ms");
Lab Step 1
Create a Main class that and main method and the following variables:
Create a class called Populate that implements runnable
With the following:
Lab Step 2
This is where the work starts
We will create the single thread version first (in the main class)
We need a method to create an a
ay of a given size
And add a numbers to each position (Random 0 -999)
Another method will loop and create the specified number of a
ay and return the sum of all of them combined
We will do this as a team
Lab Step 3
Repeat – but create multiple threads
We will do this as a team
Always10
Usually8
Often6
Generally4
Sometimes3
Seldom2
Rarely1
Never0

Java Concu
ency Lab Extend Thread
Java Concu
ency
Lab Extend Thread
Presenter Name
Agenda
9/3/20XX
What is the Thread Class
Extending the Thread Class Simple Example
Presentation Title
2
Introduction
A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.
Presentation Title
3
Introduction
A single java process can have many threads.
The main process can spawn thread, or a thread can spawn a new thread!
Presentation Title
4
Introduction
We can define a thread in the following two ways:
By extending Thread class
By implementing Runnable interface
5
Thread
Thread
(Class)
Runnable
(Interface)
un()
method
Ove
ide
Extends
Implements
When to extending the thread class
In General, you would rarely need to do this.
Extending the Thread class is reserved for creating new thread functionality.
If I need to change how a thread does something which given the nature of this, it would be fraught with danger.
Presentation Title
6
Thread Class
Thread a line of execution within a program.
Each program can have multiple associated threads.
Each thread has a priority which is used by the thread scheduler to determine which thread must run first.
Java provides a thread class that has various method calls in order to manage the behavior of threads by providing constructors and methods to perform operations on threads.
Presentation Title
7
Thread.sleep()
Causes the cu
ently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
The thread does not lose ownership of any monitors.(Don’t wo
y about this yet)
8
join()
The join() method which allows one thread to wait until another thread completes its execution.
If t is a Thread object whose thread is cu
ently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program.
9
Thread 1
Thread 2
Main Thread
Run
Run
Join
Join
Extend the Thread Class
Simple Example
Note
In the case of multithreading, we can't predict the exact order of output because it will vary from system to system or JVM to JVM.
Presentation Title
10
This thread class will run a loop until it finds a number divisibly by the one passed into the constructor.
It calls sleep for 50 milliseconds
It keep track of home many random number it generates
Example Outputs
This main method creates three threads.
It then call run which will spawn them.
It calls join on each thread.
This cause main to wait for the thread to finish
This means its run method must complete – so it is finishes
Then it prints and aggregates the results.
Lab Step 1
Create a class that Extends thread
Ove
ide the run method
Have a loop that runs until it find 10 even numbers
Random Generate a number XXXXXXXXXX
Keep track of the count of the even and odd number.
Sleep for the number milliseconds based on the random numbe
Lab Step 2
Create three instances of your thread class
Store them in an A
ayList
Run all three
Join all three
Get the counts of odds and evens
Print the average for all threads
Always10
Usually8
Often6
Generally4
Sometimes3
Seldom2
Rarely1
Never0
Answered 1 days After Oct 27, 2022

Solution

Vikas answered on Oct 29 2022
57 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here