libraries
play

Libraries In C++ its possible to create static libraries and shared - PowerPoint PPT Presentation

Libraries In C++ its possible to create static libraries and shared libraries Static libraries (end in .a) are combined/linked into an executable Executables are large. If library is updated in a binary compatible way, programs


  1. Libraries ● In C++ it’s possible to create static libraries and shared libraries ➤ Static libraries (end in .a) are combined/linked into an executable • Executables are large. If library is updated in a binary compatible way, programs still need to be relinked ➤ Dynamic/shared libraries (end in .so) are “fetched” when the program is run • Executables are small, updates can happen at run time ➤ Location of library can be made part of the executable (e.g., in Solaris, not in Linux as far as I know, -R in linker). • Alternative is to use environment LD_LIBRARY _PATH ➤ DLL in windows is dynamically linked aka shared 16.1 Duke CPS 108

  2. Java libraries ● Create a jar file of .class files, usually from several packages ➤ No need to unjar the archive, java will “link”, or in Java parlance the Classloader will find, classes in a jar file ➤ The CLASSPATH variable is like LD_LIBRARY_PATH ➤ Typically several packages are part of the jar file, see swingall.jar/swing.jar for 1.1 use of swing ● Java archives can also be a source of .gif files, data files (e.g., html/text help) ➤ Use ClassLoader.getSystemResource to fetch from jar files in CLASS_PATH ● Related: ResourceBundle, PropertyResourceBundle ➤ Helps in internationalization or in customizing GUIs 16.2 Duke CPS 108

  3. Adding ActionListeners (or others) ● Heuristic, keep the source of the action and the handler of the action near each other in codespace ➤ Use an anonymous inner class to handle simple actions JButton next = new JButton("next>"); next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { nextDisplay(); } }); ➤ Alternatively use a Command object, attach near source JMenuItem urlMI = new JMenuItem("URL"); urlMI.addActionListener( new ReadURLCommand(myController,this)); 16.3 Duke CPS 108

  4. Model, View, Controller (MVC) ● Related to observer/observable ➤ The model is the data, the guts, the application ➤ The view is how the user sees the data • There can be more than one view, e.g., bar char, pie chart ➤ The controller mediates between the model and the view ● In Swing, MVC is prevalent but the View and the Controller are often combined, model is still separate ➤ Allows some concurrency, the model can be accessed concurrently (perhaps), view updated, doesn’t always work ● Observable/Observer models ActionEvent/ActionListener ➤ Observers register themselves with observables, are notified when they (observer) need to be updated 16.4 Duke CPS 108

  5. Inner classes ● Can access fields/instance variables in enclosing class ➤ If enclosing close is Foo , the Foo object in the inner class is referenced with Foo.this , so local variable myName is myName Foo.this.myName ➤ Can also access local variables/parameters in a function, but these must be final • Like const, cannot be modified, ensures that copy doesn’t change ● Inner classes are compiled as separate classes with access to another class’s private data – why is this strange? 16.5 Duke CPS 108

  6. Threads and concurrency: problems? ● Concurrent access to shared resources can cause problems, the computer executes very quickly, sequential intuition goes out the window ➤ complete understanding includes starvation , fairness , deadlock • starvation: does some thread never execute • fairness: do all threads eventually get a chance to execute • deadlock: all threads blocked, waiting on a resource ● Operating Systems and Dining Philosophers ➤ five people in a circle, chopstick between each person, algorithm to ensure that everyone thinks and eats (pick up one chopstick at a time?) 16.6 Duke CPS 108

  7. Java threads, Platform problems ● In general avoid tight thread loops that only compute ➤ use Thread.sleep(), use Thread.yield() • yield explicitly gives other threads a chance to execute, but doesn’t pause ● On Solaris machines there’s no guarantee (not required in Java) that threads scheduled with time-slicing, so use yield to ensure some degree of fairness ● Try to avoid dealing with shared resources as in Producer/Consumer problem, hard to get right ➤ Scooter example: what do robots do? • Queue up at destination: factory/robot • Is the destination threaded? Who processes arrivals and departures? 16.7 Duke CPS 108

  8. Monitors, condition variables ● All objects with at least one synchronized method share a monitor (concept due to C.A.R. Hoare) ➤ access to synchronized methods requires acquiring a lock via the monitor, only one synchronized method is executed at a time on a per-object basis ➤ no process/Thread should hold the lock indefinitely, it’s possible to wait() when the lock is held, relinquishing monitor to other waiting threads, then re-aquire the lock ➤ when a synchronized method finishes, the lock is lost, except for recursive calls --- monitors are re-entrant ➤ in general when using wait(), use notifyAll() rather than notify() ➤ interfaces can’t be synchronized 16.8 Duke CPS 108

Recommend


More recommend