Lesson Plan

advertisement

Lesson Plan

Course Title: Advanced Computer Programming

Session Title: Concurrency

Lesson Duration: 2-3 days

Performance Objective:

Upon completion of this assignment, the student will be able to:

1. identify and explain the concepts relating to concurrency and multithreaded processing

(multitasking)

2. implement a program that uses multitasking

Specific Objectives:

 Articulate how a programming language can support multitasking

 Identify the hardware / software connection related to concurrent programming

 Identify and define the key vocabulary used in concurrent programming

 Implement a program using concurrent programming techniques

Preparation

TEKS Correlations: 130.277 (c)

8B - identify the hardware software connection

8E - articulate how a programming language can support multitasking and exception handling

Instructor/Trainer

References:

Java API . Program documentation. Ver. 1.6. Web..

Horstmann, Cay S. Big Java . Hoboken, NJ: Wiley, 2010. Print.

Instructional Aids:

Presentation

Concurrency

Labs (KEY is provided for PreLab)

PreLab, Lab 1 and Lab 2 Handout

Student Files (Solution KEY files are included)

Sing.java, Song.java (Lab 1)

Driver.java, Ball.java, Mover.java (Lab 2)

Quizzes (Quiz and Key provided)

Concurrency Quiz

Example Source Code

RaceTest (AddRacer.java, RaceTest.java, RemoveRacer.java, ValueContainer.java)

Used for demonstrating race conditions

Materials Needed:

JavaDocs (either through internet access or saved to student’s workstation)

Copies of PreLab

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

1

Copies of Lab1, Lab2

Copies of Concurrency Quiz

Copies of example source code (having the students type in the documents is preferred)

AddRacer.java

RaceTest.java

RemoveRacer.java

ValueContainer.java

Student Files should be loaded on student computers, or provided in some manner. Be careful not to give the Students the KEY files.

Equipment Needed:

Each student will require a computer with a java compiler with a java JDK version of 1.5 or newer.

Learner

Students should have observation journals to record common errors and other observations.

Students should be comfortable with the concept of interfaces and how to implement an interface.

MI

Introduction

Introduction (LSI Quadrant I):

Most students will have written structured, sequential programs. When a program copies a file from one location to another, you see that a progress bar slowly fills up while at the same time the program copies the file from one location to another.

Also, operating systems can both run multiple windows on the screen at one time.

Have students suggest some ideas on how programs can do many things at once.

Outline

MI Outline (LSI Quadrant II):

Concurrency Presentation

Slides

Prelabs

Example Code

Uses

Multiple tasks

Animation

Instructor Notes:

Concurrency can be a very challenging concept for some students to grasp. You may want to look up resources on the internet to supplement the descriptions given in this material.

The labs only cover the surface level of concurrency, advanced students should be considered to explore more advanced uses of threading such as implementing a clock.

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

2

MI

Application

Guided Practice (LSI Quadrant III):

The example code files are intended to be read by the students to get examples of how to use the code. There is no assignment to change or modify, but to read and ask questions. It is strongly encouraged that the students enter the code and observe the results. Seeing working code can greatly increase their success in understanding future code.

Have students comment about the examples to see if they can understand what is going on. Sometimes they can, sometimes they can't, but in the end, they do get a deeper understanding of what is going on.

Students should enter the example code, run it and record observations about what the code does. If the students are using an IDE that allows line by line debugging

(such as BlueJ or Eclipse), it would be a good idea to trace the code, line by line.

(please see the documentation for your IDE on how to do this).

MI

MI

Independent Practice (LSI Quadrant III):

Following the presentation, students should be provided with the labs to complete on their own.

Summary

Review (LSI Quadrants I and IV):

MI

Students will incorporate the concepts introduced by completing the hands on labs.

It would also be a good idea to have the students keep an observation journal to record observations of common errors they encounter and what kinds of results they are getting.

Evaluation

Informal Assessment (LSI Quadrant III):

MI

You may use the PreLab to assess whether or not the students are progressing well.

The PreLab is important in determining whether or not the students are ready to complete the lab.

Formal Assessment (LSI Quadrant III, IV):

MI

Concurrency Quiz

Concurrency Lab 1 and Lab 2

Extension

Extension/Enrichment (LSI Quadrant IV):

Students can create interactive artificial intelligence, complex animations, or try and speed up programs they’ve already written using these techniques.

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

3

Icon MI

Verbal/

Linguistic

Logical/

Mathematical

Visual/Spatial

Musical/

Rhythmic

Bodily/

Kinesthetic

Intrapersonal

Interpersonal

Naturalist

Existentialist

Teaching Strategies

Lecture, discussion, journal writing, cooperative learning, word origins

Problem solving, number games, critical thinking, classifying and organizing,

Socratic questioning

Mind-mapping, reflective time, graphic organizers, color-coding systems, drawings, designs, video,

DVD, charts, maps

Use music, compose songs or raps, use musical language or metaphors

Use manipulatives, hand signals, pantomime, real life situations, puzzles and board games, activities, role-playing, action problems

Reflective teaching, interviews, reflective listening, KWL charts

Cooperative learning, roleplaying, group brainstorming, crosscultural interactions

Natural objects as manipulatives and as background for learning

Socratic questions, real life situations, global problems/questions

Personal Development Strategies

Reading, highlighting, outlining, teaching others, reciting information

Organizing material logically, explaining things sequentially, finding patterns, developing systems, outlining, charting, graphing, analyzing information

Developing graphic organizers, mind-mapping, charting, graphing, organizing with color, mental imagery (drawing in the mind’s eye)

Creating rhythms out of words, creating rhythms with instruments, playing an instrument, putting words to existing songs

Moving while learning, pacing while reciting, acting out scripts of material, designing games, moving fingers under words while reading

Reflecting on personal meaning of information, studying in quiet settings, imagining experiments, visualizing information, journaling

Studying in a group, discussing information, using flash cards with other, teaching others

Connecting with nature, forming study groups with like minded people

Considering personal relationship to larger context

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

4

NAME: _________________________ DATE: ______________ PERIOD: ____________

Concurrency – PreLab

Purpose: At the end of this lab, you should be able to construct your own threaded tasks and understand the basic concepts behind concurrent programming. To fully understand more complex programming topics such as multi-threaded programming, it often helps to look at code that does something. In this lab, you are going to examine the “RaceTest” source code which demonstrates an example race condition in effect.

Materials: You will require a java compiler and editor, a web browser with access to search engines, the RaceTest example source code and the documents for this lab.

Directions: Ensure the RaceTest example source compiles. Once you have successfully compiled the code, examine the output and answer the following questions.

Questions:

1. What is the definition of Concurrency?

2. What is the definition of Race Condition?

3. The standard output from the program should look something like this:

Adding : 1, new total: 312

Removing : 1, new total: 311

Adding : 1, new total: 312

Removing : 1, new total: 311

Removing : 1, new total: 310

Find lines of output that do not look like this, and write examples of those lines. If you do not see differences try running the program again.

What do you think caused these errors? Explain how this might have happened.

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

5

4. Open the value container class and change the following methods:

Change public void add(int y) to public synchronized void add(int y)

Change public void add(int y) to public synchronized void add(int y)

Compile the program; then execute it several times. What changed? Why do you think it changed?

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

6

NAME: _________________________ DATE: ______________ PERIOD: ____________

Concurrency – PreLab KEY

Purpose: At the end of this lab, you should be able to construct your own threaded tasks and understand the basic concepts behind concurrent programming. To fully understand more complex programming topics such as multi-threaded programming, it often helps to look at code that does something. In this lab, you are going to examine the “RaceTest” source code which demonstrates an example race condition in effect.

Materials: You will require a java compiler and editor, a web browser with access to search engines, the RaceTest example source code and the documents for this lab.

Directions: Ensure the RaceTest example source compiles. Once you have successfully compiled the code, examine the output and answer the following questions.

Questions:

1. What is the definition of Concurrency?

Concurrency allows multiple tasks to be accomplished at once using threads of execution.

2. What is the definition of Race Condition?

When two threads try to use variables on the same object at the same time

3. The standard output from the program should look something like this:

Adding : 1, new total: 312

Removing : 1, new total: 311

Adding : 1, new total: 312

Removing : 1, new total: 311

Removing : 1, new total: 310

Find lines of output that do not look like this, and write examples of those lines. If you do not see differences try running the program again.

Output should be intermixed giving different results than those shown.

Check the students work and screen information for guidance

What do you think caused these errors? Explain how this might have happened.

The code is forcing a race condition and things are being printed before another thread is done printing its line.

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

7

4. Open the value container class and change the following methods:

Change public void add(int y) to public synchronized void add(int y)

Change public void add(int y) to public synchronized void add(int y)

Compile the program; then execute it several times. What changed? Why do you think it changed?

The race condition is fixed through the use of the synchronized key word.

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

8

NAME: _________________________ DATE: ______________ PERIOD: ____________

Concurrency – Lab 1

Purpose: Practice creating threaded tasks in a computer program.

Materials: Sing.java and Song.java

Directions:

In the main method of Sing, create an instance of Song that sings the chorus several times, put it in a thread, then start the thread, eg:

Song song = new Song(2);

Thread t = new Thread(song); t.start();

Compile and run the program. Record your observations of what happens when it runs.

Create ten song threads. How does the behavior change?

Increase the number of Song iterations per Thread to 20. Record your observations.

Try adding a different song mixed with the original song.

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

9

NAME: _________________________ DATE: ______________ PERIOD: ____________

Concurrency – Lab 2

Purpose: Use threads to create an animation.

Materials: Driver.java, Mover.java, Ball.java

Directions:

A. To create motion, use a threaded task to update the ball object.

For the first part of this lab, you will want to work only on the Mover.java file. No changes are required in either Driver.java or Ball.java.

1. The Mover will require two objects: the JPanel that is drawing the ball and a copy of the ball itself. Create two variables in the Mover class to store these two objects.

2. Using the constructor in Mover, store the two objects.

3. Inside the “forever loop” in the run method of the Mover class, update the ball; then repaint the JPanel ( JPanels have a function called repaint() you use to achieve this). Finally, in the loop you should add a pause. Thread.sleep() is a good method for doing this.

Think about it: Why do you think the run() method of Mover requires a “forever loop”?

B. Once you have one ball moving around the screen, try adding multiple balls.

*For an extreme challenge, try getting the balls to “bounce” off each other. (This is extremely difficult).

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

10

NAME: _________________________ DATE: ______________ PERIOD: ____________

Concurrency Quiz

1. When would you use concurrency in a program? a. To create animation b. To save a file while simultaneously working on a document c. To allow two users to work on a program at the same time d. All of the Above

2. What property of a programming language is not a core component of concurrency? a. Threads b. Scheduling c. Time-sharing d. Remote Method Invocation

3. What computer hardware feature can specifically enable concurrent programs to execute faster? a. Multi-Core processor b. Internal Clock c. ALU d. Video Card

4. What is the Java interface used to implement Threaded tasks? a. Thread b. Concurrent c. Runnable d. None of the Above

5. What is a race condition? a. When one thread tries to finish executing before another thread b. When two threads try to modify or access the same object simultaneously c. When one thread attempts to access the processor before another thread d. None of the Above

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

11

6. What Java keyword can be used to implement a Thread lock? a. Synchronized b. Lock c. Mutex d. None of the Above

7. Which of the following collections are thread safe? a. Vector b. Stack c. Hashtable d. All of the Above

8. What causes dead lock? a. Two or more threads that can’t decide on how to vote on something b. Two or more threads trying to access multiple resources that are locked by other threads c. When a thread gets stuck in the processor d. None of the Above

9. What is one method of avoiding thread dead lock? a. Elect better threads b. Force threads to release locks c. Always access objects in the same order d. None of the Above

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

12

NAME: _________________________ DATE: ______________ PERIOD: ____________

Concurrency Quiz KEY

1. When would you use concurrency in a program? a. To create animation b. To save a file while simultaneously working on a document c. To allow two users to work on a program at the same time d. All of the Above

2. What property of a programming language is not a core component of concurrency? a. Threads b. Scheduling c. Time-sharing d. Remote Method Invocation

3. What computer hardware feature can specifically enable concurrent programs to execute faster? a. Multi-Core processor b. Internal Clock c. ALU d. Video Card

4. What is the Java interface used to implement Threaded tasks? a. Thread b. Concurrent c. Runnable d. None of the Above

5. What is a race condition? a. When one thread tries to finish executing before another thread b. When two threads try to modify or access the same object simultaneously c. When one thread attempts to access the processor before another thread d. None of the Above

6. What Java keyword can be used to implement a Thread lock? a. Synchronized b. Lock c. Mutex d. None of the Above

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

13

7. Which of the following collections are thread safe? a. Vector b. Stack c. Hashtable d. All of the Above

8. What causes dead lock? a. Two or more threads that can’t decide on how to vote on something b. Two or more threads trying to access multiple resources that are locked by other threads c. When a thread gets stuck in the processor d. None of the Above

9. What is one method of avoiding thread dead lock? a. Elect better threads b. Force threads to release locks c. Always access objects in the same order d. None of the Above

IT: Advanced Computer Programming : [Concurrency] Plan

Copyright © Texas Education Agency, 2013

14

Download