SlideShare une entreprise Scribd logo
1  sur  36
Best practices in Java and Swing
Arne Bachmann




                                                                     Folie 1
                                   Vortrag > Autor > Dokumentname > 09.11.2005
Topics


  Arrays, Collections and Loops

  Enumerations

  The final keyword

  Threading
       In Swing

  Singleton

  Tools


                                                                    Folie 2
                                  Vortrag > Autor > Dokumentname > 09.11.2005
Arrays, Collections and Loops


    "Foreach" style loop



void listLines(String[] lines) {
       PrintStream ps = new PrintStream(
              new FileOutputStream("C:test.out"));

         for (String line: lines) {
               ps.println(line);
         }

         ps.close();
}



                                                                               Folie 3
                                             Vortrag > Autor > Dokumentname > 09.11.2005
Arrays, Collections and Loops


    "Map-Entry" style loop – similar to ".items()" in Python



String listItems(Map<String, Long> collection) {
    StringBuilder sb = new StringBuilder();

     for (Entry<String, Long> entry: collection.entrySet())
{
               sb.append(entry.getKey())
                 .append(" maps to ")
                 .append(entry.getValue());
                 .append("n");
         }

    return sb.toString();
}


                                                                                                 Folie 4
                                                               Vortrag > Autor > Dokumentname > 09.11.2005
Enumerations




                                                 Folie 5
               Vortrag > Autor > Dokumentname > 09.11.2005
Enumerations


  Don't try to serialize enums
        don't provide a private static final long serialVersionUID

  Use enum instead of true/false where possible

import static my.package.Visibility.VISIBLE;
import static my.package.Visibility.HIDDEN;

display.setVisible(VISIBLE);
display.setVisible(HIDDEN);




                                                                                      Folie 6
                                                    Vortrag > Autor > Dokumentname > 09.11.2005
Enumerations


  Don't try to serialize enums
        don't provide a private static final long serialVersionUID

  Use enum instead of true/false where possible

import static my.package.Visibility.VISIBLE;
import static my.package.Visibility.HIDDEN;
Import static my.package.Visibility.SEMI_TRANSPARENT;

display.setVisible(SEMI_TRANSPARENT);




                                                                                      Folie 7
                                                    Vortrag > Autor > Dokumentname > 09.11.2005
Enumerations


  Combine settings by using enumSet



enum ELogCapability {
       Console,
       Graphical,
       File;
}

// Constructor
MyLogger(EnumSet<ELogCapability> logTypes) {
       if (logTypes.contains(ELogCapability.Console))
{
              // ...
       } else if ...
}
                                                                              Folie 8
                                            Vortrag > Autor > Dokumentname > 09.11.2005
Enumerations


    Powerful enumerations – Part 1: The less known constructor



enum EVariableDataType {

     Integer ("long"),
     Real ("double"),
     File ("file");

     private final String type;

     private EVariableDataType(String aType) {
         type = aType;
     }

}
                                                                                            Folie 9
                                                          Vortrag > Autor > Dokumentname > 09.11.2005
Enumerations


  Powerful enumerations – Part 2: The trick

private static final Map<String, EVariableDataType>
     TYPES = new Hashtable<String, EVariableDataType>();

static {
       TYPES.put(Integer.type, Integer);
       TYPES.put(Real.type, Real);
       TYPES.put(File.type, File);
}

public static EVariableDataType typeOf(String forName) {
       return TYPES.get(forName.toLowerCase());
}


                                                                               Folie 10
                                              Vortrag > Autor > Dokumentname > 09.11.2005
Final




                                         Folie 11
        Vortrag > Autor > Dokumentname > 09.11.2005
Final


      Use final as often as possible

final class ExampleClass {

    void example(final File source) {
       final File[] files = source.listFiles();
       for (final File file: files) {
            final String name =
file.getAbsolutePath();
            try {
               file.delete();
            } catch (final IOException e) {
               logger.error(e);
            }
       }
     }

}
                                                                                   Folie 12
                                                  Vortrag > Autor > Dokumentname > 09.11.2005
Final


    Constant value initialization from the constructor



protected class MyClass {

         final private String param;

         MyClass(final String p) {
                param = p;
         }

}




                                                                                          Folie 13
                                                         Vortrag > Autor > Dokumentname > 09.11.2005
Variable number of arguments


       Beware of varargs methods:
           Can create very hard to find errors
Long[] createLongArray(final long first) {
       return new Long[] { first };
}

Long[] createLongArray(final long first, final long second) {
       return new Long[] { first, second };
}

Long[] createLongArray(long... args) {
       final List<Long> list = new ArrayList<Long>();
       for (final Long l: args) {
               list.add(l);
       }
       return list.toArray(new Long[0]); // or: new Long[] {}
}
                                                                                       Folie 14
                                                      Vortrag > Autor > Dokumentname > 09.11.2005
Collections




                                               Folie 15
              Vortrag > Autor > Dokumentname > 09.11.2005
Collections


  Always type collections (we're in Java SE 5, which already came 2004)
       e.g.: List<Pair<String, Wrapper<ELogCapability>[]>> list;

  Hashtable<T, U>
       Allows only non-null keys and values
       Synchronized
  HashMap<T, U>
       Allows null for both keys and values
       Unsynchronized

  Use new ConcurrentHashMap() for Collections.synchronizedMap()
       Internally synchronized



                                                                                         Folie 16
                                                        Vortrag > Autor > Dokumentname > 09.11.2005
Singleton




                                             Folie 17
            Vortrag > Autor > Dokumentname > 09.11.2005
Singleton


    Singleton – Often implemented wrong:
public class MySingleton {

     private static MySingleton instance;

     private MySingleton() {
         // initialization
     }

     public static MySingleton getInstance() {
         if (instance == null) {
             instance = new MySingleton();
         }
         return instance;
     }

}

                                                                                  Folie 18
                                                 Vortrag > Autor > Dokumentname > 09.11.2005
Singleton


    Singleton – This works
public class MySingleton {

     private static MySingleton instance;

     private MySingleton() {
         // initialization
     }

     public static synchronized MySingleton getInstance() {
         if (instance == null) {
             instance = new MySingleton();
         }
         return instance;
     }

}

                                                                                   Folie 19
                                                  Vortrag > Autor > Dokumentname > 09.11.2005
Singleton


      Singleton – Use synchronization of the classloader

public class MySingleton2 {

     final static private MySingleton2 instance = new MySingleton2();

     private MySingleton2() {
         // initialization
     }

     public MySingleton2 getInstance() {
         return instance;
     }

}



                                                                                            Folie 20
                                                           Vortrag > Autor > Dokumentname > 09.11.2005
Threading




                                             Folie 21
            Vortrag > Autor > Dokumentname > 09.11.2005
Threading


       Useful: Set UncaughtExceptionHandler


class MyHandler implements UncaughtExceptionHandler {
    public void uncaughtException(final Thread t, final Throwable e) {
        logger.error(t.getName() + ": " + e.getMessage());
    }
}

void newThread() {
    final Thread thread = new Thread(new Runnable() {
        public void run() {
            // do something in the thread
        }
    });
    thread.setUncaughtExceptionHandler(new MyHandler.getInstance());
}

                                                                                       Folie 22
                                                      Vortrag > Autor > Dokumentname > 09.11.2005
Threading


   How to create standard threads
        Dont' use new Thread(…) anymore!

final ExecutorService single;



single = Executors.newSingleThreadExecutor();



single.execute(new Runnable() {
    public void run() {
        // anonymous code
    }
});

                                                                                 Folie 23
                                                Vortrag > Autor > Dokumentname > 09.11.2005
Threading


   How to create standard threads
        Dont' use new Thread(…) anymore!

final ExecutorService single;
final ExecutorService pool;
final ExecutorService timed;

single = Executors.newSingleThreadExecutor();
pool =   Executors.newFixedThreadPool(3);
timed = Executors.newSingleThreadScheduledExecutor();

single.execute(new Runnable() {
    public void run() {
        // anonymous code
    }
});

                                                                                  Folie 24
                                                 Vortrag > Autor > Dokumentname > 09.11.2005
Threading


  Use volatile and synchronize wisely
       Not easy to understand, find yourself some articles on that topic!

       Volatile ensures visibility of
             Variable reference (for reference types)
             Variable value (for simple types)
                  Use atomic types for long and double

             Because Java usually doesn't guarantee visibility of other
             threads' memory changes

       Synchronize explicitly uses JVM's object locking mechanism to
       synchronize (linearize) variable accesses → deadlocks possible



                                                                                            Folie 25
                                                           Vortrag > Autor > Dokumentname > 09.11.2005
Threading


  Avoid using anonymous classes where applicable
       Refactor to named classes step-by-step

  How to avoid the "synthetic accessor method" problem?
       Named inner classes, see next slide




                                                                                           Folie 26
                                                          Vortrag > Autor > Dokumentname > 09.11.2005
Threading


      "Read access is emulated by a synthetic accessor" (Eclipse warning)

public class WrongSyntheticAccessorExample extends JDialog {

     private String name;

     protected JPanel createPanel() {
         final JPanel panel = new JPanel();
         final JButton button = new JButton("Huh?");
         button.addActionListener(new ActionListener() {
             public void actionPerformed(final ActionEvent evt) {
                 button.setText(button.getText() + name);
             }
         });
         return panel;
     }

}
                                                                                              Folie 27
                                                             Vortrag > Autor > Dokumentname > 09.11.2005
Threading


  Solution: Inner classes
  public class SyntheticAccessorExample extends JDialog {

      private class InnerClass implements ActionListener {
          private final String copyOfName;
          private final JButton buttonRef;
          InnerClass(final String name, final JButton button) {
              copyOfName = new String(name); // shallow clone
              buttonRef = button; // keep reference
          }
          public void actionPerformed(final ActionEvent evt) {
              buttonRef.setText(buttonRef.getText() + copyOfName);
          }
      }

      protected JPanel createPanel() {
          final JPanel panel = new JPanel();
          final JButton button = new JButton("Huh?");
          button.addActionListener(new InnerClass("Done!", button));
          return panel;
      }

  }

                                                                                                Folie 28
                                                               Vortrag > Autor > Dokumentname > 09.11.2005
Threading in Swing


  The Event Dispatch Thread (EDT)
       Almost everything GUI related must happen in this thread!
            Otherwise: GUI not updated, blocking or unintelligible results
            Exception: first static call from main or init
            Exception: repaint() … and listener (de-) registration

       Events by GUI components are always automatically on the EDT

       Put everything CPU or time consuming to other threads (executors)
            And provide callbacks for GUI updates

       Q: How to build up a complex GUI while staying responsive?


                                                                                            Folie 29
                                                           Vortrag > Autor > Dokumentname > 09.11.2005
Threading in Swing


  Events by GUI components are always automatically on the EDT, e.g.
       public void actionPerformed(final ActionEvent evt) {}
       public void mouseClicked(final MouseEvent e) {}


  Use javax.swing.Timer to create repeating events on the EDT:

  final Timer timer = new Timer(200 /* ms */,
                                new ActionListener() {
      public void actionPerformed(final ActionEvent evt) {
          button.setEnabled(!button.isEnabled());
      }
  });
  timer.setInitialDelay(0); // start immediately
  timer.start();



                                                                                        Folie 30
                                                       Vortrag > Autor > Dokumentname > 09.11.2005
Threading in Swing


      Put everything CPU or time consuming to other threads (executors)
           And provide callbacks for GUI updates
final JButton okB = new JButton("OK");
okB.setEnabled(false);
final ExecutorService thread =
Executors.newSingleThreadExecutor();

thread.execute(new Runnable() {
    public void run() {
       // do some time-consuming calculations here
       SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                okB.setEnabled(true);
            }
       });
       thread.shutdown(); // commence own shutdown
    }
});                                                                                           Folie 31
                                                             Vortrag > Autor > Dokumentname > 09.11.2005
Threading in Swing


  Initialization during class load or construction is possible
          OK if class loaded on EDT (e.g. new GUI(); in invokeLater())
          OK if initialization is only simple, fast and failsafe
          BUT if exceptions occur here, they are hard to track down!




                                                                                          Folie 32
                                                         Vortrag > Autor > Dokumentname > 09.11.2005
Threading in Swing


  Question: How to build up a complex GUI while staying responsive?
      A: Ensure that a splash screen is shown before building GUI?
      A: Break down GUI creation into single work tasks queued in EDT?
      A: Interrupt the thread somehow?




                                                                                        Folie 33
                                                       Vortrag > Autor > Dokumentname > 09.11.2005
Tools


  VisualVM
       Based on Netbeans platform
       Connects to VMs via JMX (really useful only since Java SE 6)
       Allows profiling, monitoring, recording, snapshotting
       Many plugins available

  Swing Explorer Library
       Detect errors in UI applications




                                                                                          Folie 34
                                                         Vortrag > Autor > Dokumentname > 09.11.2005
References


  Enumerations
      http://www.javaworld.com/community/node/3125

  Singleton
        http://www.theserverside.de/singleton-pattern-in-java/

  Threading
       http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

  Java Best practices:
       Joshua Bloch (2008): "Effective Java. Second edition."




                                                                                             Folie 35
                                                            Vortrag > Autor > Dokumentname > 09.11.2005
Questions?




             http://www.walle-derfilm.de/

Contenu connexe

Tendances

20100730 phpstudy
20100730 phpstudy20100730 phpstudy
20100730 phpstudy
Yusuke Ando
 
Clojure Interoperability
Clojure InteroperabilityClojure Interoperability
Clojure Interoperability
rik0
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
Rakesh Madugula
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
Hariz Mustafa
 

Tendances (19)

Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
NIO and NIO2
NIO and NIO2NIO and NIO2
NIO and NIO2
 
20100730 phpstudy
20100730 phpstudy20100730 phpstudy
20100730 phpstudy
 
The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185
 
The Ring programming language version 1.5.3 book - Part 32 of 184
The Ring programming language version 1.5.3 book - Part 32 of 184The Ring programming language version 1.5.3 book - Part 32 of 184
The Ring programming language version 1.5.3 book - Part 32 of 184
 
Link list
Link listLink list
Link list
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
Clojure Interoperability
Clojure InteroperabilityClojure Interoperability
Clojure Interoperability
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Perl
PerlPerl
Perl
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
Corba by Example
Corba by ExampleCorba by Example
Corba by Example
 
I phone 12
I phone 12I phone 12
I phone 12
 

Similaire à Best practices in Java and Swing

international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
smueller_sandsmedia
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011
julien.ponge
 

Similaire à Best practices in Java and Swing (20)

Oop lecture9 12
Oop lecture9 12Oop lecture9 12
Oop lecture9 12
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
The Not Java That's Not Scala
The Not Java That's Not ScalaThe Not Java That's Not Scala
The Not Java That's Not Scala
 
w10 (1).ppt
w10 (1).pptw10 (1).ppt
w10 (1).ppt
 
Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coinsoft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
 
Writing Swift code with great testability
Writing Swift code with great testabilityWriting Swift code with great testability
Writing Swift code with great testability
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Java
 
Scala Paradigms
Scala ParadigmsScala Paradigms
Scala Paradigms
 
Vba functions
Vba functionsVba functions
Vba functions
 
My Favourite 10 Things about Xcode/ObjectiveC
My Favourite 10 Things about Xcode/ObjectiveCMy Favourite 10 Things about Xcode/ObjectiveC
My Favourite 10 Things about Xcode/ObjectiveC
 
Expression trees in c#
Expression trees in c#Expression trees in c#
Expression trees in c#
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overview
 
55j7
55j755j7
55j7
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Dernier (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Best practices in Java and Swing

  • 1. Best practices in Java and Swing Arne Bachmann Folie 1 Vortrag > Autor > Dokumentname > 09.11.2005
  • 2. Topics Arrays, Collections and Loops Enumerations The final keyword Threading In Swing Singleton Tools Folie 2 Vortrag > Autor > Dokumentname > 09.11.2005
  • 3. Arrays, Collections and Loops "Foreach" style loop void listLines(String[] lines) { PrintStream ps = new PrintStream( new FileOutputStream("C:test.out")); for (String line: lines) { ps.println(line); } ps.close(); } Folie 3 Vortrag > Autor > Dokumentname > 09.11.2005
  • 4. Arrays, Collections and Loops "Map-Entry" style loop – similar to ".items()" in Python String listItems(Map<String, Long> collection) { StringBuilder sb = new StringBuilder(); for (Entry<String, Long> entry: collection.entrySet()) { sb.append(entry.getKey()) .append(" maps to ") .append(entry.getValue()); .append("n"); } return sb.toString(); } Folie 4 Vortrag > Autor > Dokumentname > 09.11.2005
  • 5. Enumerations Folie 5 Vortrag > Autor > Dokumentname > 09.11.2005
  • 6. Enumerations Don't try to serialize enums don't provide a private static final long serialVersionUID Use enum instead of true/false where possible import static my.package.Visibility.VISIBLE; import static my.package.Visibility.HIDDEN; display.setVisible(VISIBLE); display.setVisible(HIDDEN); Folie 6 Vortrag > Autor > Dokumentname > 09.11.2005
  • 7. Enumerations Don't try to serialize enums don't provide a private static final long serialVersionUID Use enum instead of true/false where possible import static my.package.Visibility.VISIBLE; import static my.package.Visibility.HIDDEN; Import static my.package.Visibility.SEMI_TRANSPARENT; display.setVisible(SEMI_TRANSPARENT); Folie 7 Vortrag > Autor > Dokumentname > 09.11.2005
  • 8. Enumerations Combine settings by using enumSet enum ELogCapability { Console, Graphical, File; } // Constructor MyLogger(EnumSet<ELogCapability> logTypes) { if (logTypes.contains(ELogCapability.Console)) { // ... } else if ... } Folie 8 Vortrag > Autor > Dokumentname > 09.11.2005
  • 9. Enumerations Powerful enumerations – Part 1: The less known constructor enum EVariableDataType { Integer ("long"), Real ("double"), File ("file"); private final String type; private EVariableDataType(String aType) { type = aType; } } Folie 9 Vortrag > Autor > Dokumentname > 09.11.2005
  • 10. Enumerations Powerful enumerations – Part 2: The trick private static final Map<String, EVariableDataType> TYPES = new Hashtable<String, EVariableDataType>(); static { TYPES.put(Integer.type, Integer); TYPES.put(Real.type, Real); TYPES.put(File.type, File); } public static EVariableDataType typeOf(String forName) { return TYPES.get(forName.toLowerCase()); } Folie 10 Vortrag > Autor > Dokumentname > 09.11.2005
  • 11. Final Folie 11 Vortrag > Autor > Dokumentname > 09.11.2005
  • 12. Final Use final as often as possible final class ExampleClass { void example(final File source) { final File[] files = source.listFiles(); for (final File file: files) { final String name = file.getAbsolutePath(); try { file.delete(); } catch (final IOException e) { logger.error(e); } } } } Folie 12 Vortrag > Autor > Dokumentname > 09.11.2005
  • 13. Final Constant value initialization from the constructor protected class MyClass { final private String param; MyClass(final String p) { param = p; } } Folie 13 Vortrag > Autor > Dokumentname > 09.11.2005
  • 14. Variable number of arguments Beware of varargs methods: Can create very hard to find errors Long[] createLongArray(final long first) { return new Long[] { first }; } Long[] createLongArray(final long first, final long second) { return new Long[] { first, second }; } Long[] createLongArray(long... args) { final List<Long> list = new ArrayList<Long>(); for (final Long l: args) { list.add(l); } return list.toArray(new Long[0]); // or: new Long[] {} } Folie 14 Vortrag > Autor > Dokumentname > 09.11.2005
  • 15. Collections Folie 15 Vortrag > Autor > Dokumentname > 09.11.2005
  • 16. Collections Always type collections (we're in Java SE 5, which already came 2004) e.g.: List<Pair<String, Wrapper<ELogCapability>[]>> list; Hashtable<T, U> Allows only non-null keys and values Synchronized HashMap<T, U> Allows null for both keys and values Unsynchronized Use new ConcurrentHashMap() for Collections.synchronizedMap() Internally synchronized Folie 16 Vortrag > Autor > Dokumentname > 09.11.2005
  • 17. Singleton Folie 17 Vortrag > Autor > Dokumentname > 09.11.2005
  • 18. Singleton Singleton – Often implemented wrong: public class MySingleton { private static MySingleton instance; private MySingleton() { // initialization } public static MySingleton getInstance() { if (instance == null) { instance = new MySingleton(); } return instance; } } Folie 18 Vortrag > Autor > Dokumentname > 09.11.2005
  • 19. Singleton Singleton – This works public class MySingleton { private static MySingleton instance; private MySingleton() { // initialization } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(); } return instance; } } Folie 19 Vortrag > Autor > Dokumentname > 09.11.2005
  • 20. Singleton Singleton – Use synchronization of the classloader public class MySingleton2 { final static private MySingleton2 instance = new MySingleton2(); private MySingleton2() { // initialization } public MySingleton2 getInstance() { return instance; } } Folie 20 Vortrag > Autor > Dokumentname > 09.11.2005
  • 21. Threading Folie 21 Vortrag > Autor > Dokumentname > 09.11.2005
  • 22. Threading Useful: Set UncaughtExceptionHandler class MyHandler implements UncaughtExceptionHandler { public void uncaughtException(final Thread t, final Throwable e) { logger.error(t.getName() + ": " + e.getMessage()); } } void newThread() { final Thread thread = new Thread(new Runnable() { public void run() { // do something in the thread } }); thread.setUncaughtExceptionHandler(new MyHandler.getInstance()); } Folie 22 Vortrag > Autor > Dokumentname > 09.11.2005
  • 23. Threading How to create standard threads Dont' use new Thread(…) anymore! final ExecutorService single; single = Executors.newSingleThreadExecutor(); single.execute(new Runnable() { public void run() { // anonymous code } }); Folie 23 Vortrag > Autor > Dokumentname > 09.11.2005
  • 24. Threading How to create standard threads Dont' use new Thread(…) anymore! final ExecutorService single; final ExecutorService pool; final ExecutorService timed; single = Executors.newSingleThreadExecutor(); pool = Executors.newFixedThreadPool(3); timed = Executors.newSingleThreadScheduledExecutor(); single.execute(new Runnable() { public void run() { // anonymous code } }); Folie 24 Vortrag > Autor > Dokumentname > 09.11.2005
  • 25. Threading Use volatile and synchronize wisely Not easy to understand, find yourself some articles on that topic! Volatile ensures visibility of Variable reference (for reference types) Variable value (for simple types) Use atomic types for long and double Because Java usually doesn't guarantee visibility of other threads' memory changes Synchronize explicitly uses JVM's object locking mechanism to synchronize (linearize) variable accesses → deadlocks possible Folie 25 Vortrag > Autor > Dokumentname > 09.11.2005
  • 26. Threading Avoid using anonymous classes where applicable Refactor to named classes step-by-step How to avoid the "synthetic accessor method" problem? Named inner classes, see next slide Folie 26 Vortrag > Autor > Dokumentname > 09.11.2005
  • 27. Threading "Read access is emulated by a synthetic accessor" (Eclipse warning) public class WrongSyntheticAccessorExample extends JDialog { private String name; protected JPanel createPanel() { final JPanel panel = new JPanel(); final JButton button = new JButton("Huh?"); button.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { button.setText(button.getText() + name); } }); return panel; } } Folie 27 Vortrag > Autor > Dokumentname > 09.11.2005
  • 28. Threading Solution: Inner classes public class SyntheticAccessorExample extends JDialog { private class InnerClass implements ActionListener { private final String copyOfName; private final JButton buttonRef; InnerClass(final String name, final JButton button) { copyOfName = new String(name); // shallow clone buttonRef = button; // keep reference } public void actionPerformed(final ActionEvent evt) { buttonRef.setText(buttonRef.getText() + copyOfName); } } protected JPanel createPanel() { final JPanel panel = new JPanel(); final JButton button = new JButton("Huh?"); button.addActionListener(new InnerClass("Done!", button)); return panel; } } Folie 28 Vortrag > Autor > Dokumentname > 09.11.2005
  • 29. Threading in Swing The Event Dispatch Thread (EDT) Almost everything GUI related must happen in this thread! Otherwise: GUI not updated, blocking or unintelligible results Exception: first static call from main or init Exception: repaint() … and listener (de-) registration Events by GUI components are always automatically on the EDT Put everything CPU or time consuming to other threads (executors) And provide callbacks for GUI updates Q: How to build up a complex GUI while staying responsive? Folie 29 Vortrag > Autor > Dokumentname > 09.11.2005
  • 30. Threading in Swing Events by GUI components are always automatically on the EDT, e.g. public void actionPerformed(final ActionEvent evt) {} public void mouseClicked(final MouseEvent e) {} Use javax.swing.Timer to create repeating events on the EDT: final Timer timer = new Timer(200 /* ms */, new ActionListener() { public void actionPerformed(final ActionEvent evt) { button.setEnabled(!button.isEnabled()); } }); timer.setInitialDelay(0); // start immediately timer.start(); Folie 30 Vortrag > Autor > Dokumentname > 09.11.2005
  • 31. Threading in Swing Put everything CPU or time consuming to other threads (executors) And provide callbacks for GUI updates final JButton okB = new JButton("OK"); okB.setEnabled(false); final ExecutorService thread = Executors.newSingleThreadExecutor(); thread.execute(new Runnable() { public void run() { // do some time-consuming calculations here SwingUtilities.invokeLater(new Runnable() { public void run() { okB.setEnabled(true); } }); thread.shutdown(); // commence own shutdown } }); Folie 31 Vortrag > Autor > Dokumentname > 09.11.2005
  • 32. Threading in Swing Initialization during class load or construction is possible OK if class loaded on EDT (e.g. new GUI(); in invokeLater()) OK if initialization is only simple, fast and failsafe BUT if exceptions occur here, they are hard to track down! Folie 32 Vortrag > Autor > Dokumentname > 09.11.2005
  • 33. Threading in Swing Question: How to build up a complex GUI while staying responsive? A: Ensure that a splash screen is shown before building GUI? A: Break down GUI creation into single work tasks queued in EDT? A: Interrupt the thread somehow? Folie 33 Vortrag > Autor > Dokumentname > 09.11.2005
  • 34. Tools VisualVM Based on Netbeans platform Connects to VMs via JMX (really useful only since Java SE 6) Allows profiling, monitoring, recording, snapshotting Many plugins available Swing Explorer Library Detect errors in UI applications Folie 34 Vortrag > Autor > Dokumentname > 09.11.2005
  • 35. References Enumerations http://www.javaworld.com/community/node/3125 Singleton http://www.theserverside.de/singleton-pattern-in-java/ Threading http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html Java Best practices: Joshua Bloch (2008): "Effective Java. Second edition." Folie 35 Vortrag > Autor > Dokumentname > 09.11.2005
  • 36. Questions? http://www.walle-derfilm.de/