Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE-2811 Software Component Design

Similar presentations


Presentation on theme: "SE-2811 Software Component Design"— Presentation transcript:

1 SE-2811 Software Component Design
6/24/2018 SE-2811 Software Component Design Week 2, Day 1 Design Goal: favor composition over inheritance Design Goal: program to an interface Design Goal: increase cohesion Design Goal: decrease coupling Lab 1 demo due during Lab 2 period. Packet due shortly thereafter. 17q ,7-11,13-14,17 SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Dr. Josiah Yoder

2 Coupling vs. Cohesion Module 4 shows what we are trying to avoid: low cohesion and high coupling (more circles==lower) (more lines==higher) SE-2811 Dr. Mark L. Hornick

3 Alternative forms of strategy pattern
See code examples SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

4 What’s a Thread? First, let’s define Process:
SE-2811 6/24/2018 What’s a Thread? First, let’s define Process: A Process is most easily understood as a program or application running on your PC A process generally has a complete, private set of basic run-time resources, in particular: SE-2811 Dr. Yoder

5 SE-2811 6/24/2018 The JVM works with the OS to create Processes and Threads The underlying OS provides the essential multiprocessing support SE-2811 Dr. Yoder

6 SE-2811 6/24/2018 Modern operating systems are all capable of running multiple Processes simultaneously (On single-CPU PC’s) each Process runs individually for a discrete time period while one Process runs, other Processes sleep The Process currently executing changes very rapidly - every few milliseconds Operating systems use a Scheduler (basically, an Interrupt Service Routine (ISR) that executes on a timer interrupt) to distribute CPU time among Processes The net effect is that you (the user) observe all processes running simultaneously and continuously SE-2811 Dr. Yoder

7 SE-2811 6/24/2018 When you run a Java application, the JVM creates a Process and a Primary Thread The Primary Thread begins executing the main() method in the main class Note: other java programs, like applets, begin execution with an init() method If no other Threads are created, the Process terminates when the Primary Thread terminates That is, when there are no more instructions to execute on that Thread SE-2811 Dr. Yoder

8 SE-2811 6/24/2018 Threads wind their way through the code until they run out of instructions to execute public class App{ public static void main(String[] args) { App me = new App(); me.method_A(); } private void method_A() { // more code here method_B(); return; private void method_B() { private void method_C() { SE-2811 Dr. Yoder

9 Where do other Threads come from?
SE-2811 6/24/2018 Where do other Threads come from? You implicitly create additional Threads when you write a Swing-based application Java applications that create and display windows cause Swing to create additional threads You implicitly create additional Threads when you use various Java utility classes Using the Timer class causes a Thread to be created You can explicitly create additional Threads and control their execution SE-2811 Dr. Yoder

10 You already know how to create a multi-threaded app using Swing
SE-2811 6/24/2018 You already know how to create a multi-threaded app using Swing Create a JFrame window containing JButtons, JTextField, etc. Connect the JButtons etc to an ActionListener Make the window visible Once the window is visible, a second Thread is created All calls to actionPerformed() occur on the second Thread The Event-Dispatching Thread SE-2811 Dr. Mark L. Hornick Dr. Yoder

11 Using a javax.swing.Timer is fairly straighforward:
SE-2811 6/24/2018 Using a javax.swing.Timer is fairly straighforward: Timer timer = new Timer(timeoutPeriod, eventHandler); timer.start(); The eventHandler argument to the constructor is a reference to a class that implements Timer ActionListener That is, eventHandler contains an actionPerformed() method. This is similar to how Swing events are handled Whenever the Timer generates a timeout event, the JVM invokes actionPerformed() on another thread JVM uses the Event Dispatch thread when available; otherwise a “worker” thread is created When describing this slide, be sure to describe what happens when start is called: After timeoutePeriod, eventHandler is called. Then it repeats… There's also a java.util.Timer. Use it if the basic event-dispatch thread handling is not enough for your application. It is a little trickier to handle multithreading correctly with java.util.Timer. SE-2811 Dr. Mark L. Hornick Dr. Yoder

12 Explicitly creating additional Threads is pretty easy:
SE-2811 6/24/2018 Explicitly creating additional Threads is pretty easy: Thread t = new Thread( r ); t.start(); The r argument to the Thread constructor is a reference to a class that implements the Runnable interface Runnable declares a single method: public void run() When the Thread’s start() method is called, the instructions in the run() method begin executing on the new thread. The start() method returns essentially immediately; it does not wait for the started thread to finish execution. Be sure to note: if you call t.run(), it will NOT start the thread. It will just call run directly. Better yet, demo in class and do Ctrl-B to show source code. SE-2811 Dr. Yoder


Download ppt "SE-2811 Software Component Design"

Similar presentations


Ads by Google