Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 121Multithreading2/19/20092Opening Discussion■Let's look at solutions to the interclass problem.■Do you have any questions about the reading?■Do you have any questions about the assignment?■What is the most significant change in computer hardware in the last four years? What impact does this change have on developers?3Minute Essay Comments■The drawing program is independent from the game. That's part of the point.■How do we draw in the drawing program?■JTree stores the things that we are drawing.■How the command processor works. Really this is how the ArrayMap works.■Use of commands in drawing program.■Tips for navigating the API.4Threads■Virtually all the programming you have done so far has been in a single thread of execution. That is to say that the program goes from one line to the next doing one at a time in order. In a program with multiple threads the same thing happens, but in multiple places at once.5Why Threads?■On a machine with a single processor and a single core threads simply give the impression of two things happening at once. With the widespread arrival of multicore processors, most of the new machines have the ability to actually do two or more things at once, assuming programs have more than one thread.■For at least a while, the future is about adding more cores to processors so software is going to have to change and that means programmers have to change as well.6The Thread Class■Most modern languages have the ability to do multithreading. It is easier in some than in others.■Java provides a simple way of doing this with the java.lang.Thread class.■This class can also be useful even if you aren’t using multiple threads because it has static methods that will impact the behavior of the current thread.■The Thread class has been in Java since the language was created. Java 5 added a newer library to help with this task. We will cover it later in the semester.7Spawning Threads■To start a new thread, you simply need to create a Thread object and pass it an instance of a java.lang.Runnable object.■Runnable is an interface with one method in it, run. When the thread object’s start method is called, the other thread becomes active, and it will begin execution at the run method of the Runnable object. Control returns to the original thread and they execute in parallel.8Thread Problems■The primary problem one runs into with multithreaded programs is that threads share memory and more than one thread can access a piece of memory at once. This isn't a problem if they are just reading, but if any thread is writing you can have bad situations.■An extreme condition would be to consider two threads operating on an array. Worst case is both are sorting the array at the same time. You could imagine one sorting while another tries to do a binary search and the results are similarly bad.■The simplest (and most common) example is a bank account where a race condition occurs.9Synchronization■The way to prevent two threads from accessing the same piece of memory at the same time is to synchronize the critical pieces of the code. You can put the synchronized keyword in front of methods or make synchronized blocks.■Each object and class in Java can have a monitor that is locked when synchronized code is being executed. Only one thread can hold the lock on the monitor at a given time. This insures that you never have two threads executing critical code on a single object at the same time.■Too much synchronization slows things down or causes deadlock.10Wait and Notify■We can get even more control over how threads behave with the wait and notify methods.■The wait method will stop the execution of a thread until some other thread tells it to continue execution. The notify and notifyAll methods are how threads tell other threads that they are supposed to wake up.■All of these must be called by a thread that holds the monitor to the object they are being invoked on. Typically that means that are called from inside synchronized code.■Wait should be called inside a while loop. Use notifyAll.11Thread Coding■Sorting code can provide a great test for threading. In this case we want to use our slow sorts so that we can actually time how long it takes them to run.■Let us create a command with some code that will create N (where N is an int variable) arrays of Doubles of length ARRAY_SIZE (you can declare that as a static final variable) then fill them with random values.■We can sort them one at a time, then refill them and sort them in N threads. Use System.nanoTime to time how long each of those takes.12Minute Essay■Why is learning how to do multithreaded programming so essential today?■Assignment #2 is due today.■Interclass Problem – Write code that prints the numbers 1-1000. Then call that code in two threads that are executing in parallel and see what happens. Try printing with both System.out and


View Full Document

TRINITY CSCI 1321 - Multithreading

Documents in this Course
Recursion

Recursion

11 pages

Iterators

Iterators

10 pages

Actors

Actors

9 pages

Recursion

Recursion

15 pages

Recursion

Recursion

10 pages

Threads

Threads

7 pages

Trees

Trees

11 pages

Load more
Download Multithreading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Multithreading and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Multithreading 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?