Skip to main content

Posts

Java Multi threaded programming basics with Reentrant lock

Java Multi threaded programming basics with Reentrant locks As we have seen in earlier post that implicit locking mechanism achieved using synchronized keyword slows down the application performance. Hence java concurrency api gives explicit locking mechanism to achieve mutual exclusion among shared object. How using explicit locks are better than implicit lock? Explicit lock acquires lock per Thread basis and not per method invoke basis. It is inverse in the case of implicit lock.   If a method is synchronized then every invocation of that method involves acquiring and releasing of lock. This process really slows down the application performance.  Hence it is always good idea to prefer reentrent lock or explicit lock to implicit lock. ReentrantLock is a class available in java.util.concurrency package lock() and unlock() methods are used used to acquire and release the lock. Condition class is used in place of Object class wait(), notifyAll() and notify(). ...

Java desktop GUI.

Graphical user interface in short GUI makes computer users easy to understand and use the application software comfortably. Almost all modern programming languages allows application developers to develop GUIs. Java has AWT/Swing as default API as part of Java Foundation classes in short JFC which are shipped with JDK. Hence these toolkits can be used directly in our applications with out adding external libraries like jars to our application's class path. But there are some other toolkits which found useful and industry endorsed in developing GUI.      SWT       JFaces [Framework for SWT]      Java FX      Swing      AWT AWT: Abstract window toolkit is available since java first version. AWT uses native operating system resources like Fonts, colors and components like table, tree, dialogues, windows etc. Few notable points about AWT: AWT is heavy weight. It uses OS resources There are l...

Java Multi threaded programming basics

All modern computer hardware architectures and operating systems are capable and optimized for multi processor system and multi threaded system.  Process Vrs Thread: Process is heavy weight where as Thread is light weight. Thread is light weight because process uses it's own address space and thread shares the Process's address space. So Threads are considered to be subset of a process. more over threads consumes less OS resources in terms of physical memory and uses CPU time at most. Junior programmers think concurrency is hard.  Experienced programmers think concurrency is easy.  Senior programmers think concurrency is hard.                                                                                    _ Java Concurrency Essentials by Martin ...

Every Java programmer must read books

Every Java programmer must read and understand the following books. 1. Effective Java 2. Concurrency in practice Effective Java: This book is authored by Joshua Bloch. He took the major role in authoring the java.util package which is [collection framework] one of the java core libraries. He explains the best practices that every programmer should consider while writing java programs. Some of them are: Contract between hashcode() and equals() of Object class when programmer overrides one of these methods in their classes Exceptions concurrency: Inter thread communication, synchronization etc Immutable Objects The above points are few among others. Concurrency in practice: This book gives complete explanation about how efficient concurrency is achieved in java programs using concurrency utility classes which were introduced in Java 5 version. This book is authored by: Brian Goetz Tim peierls Joshua Bloch Joseph Bowbeer David Holmes and Doug Lea ...

Simple program which demonstrates Swing/AWT Grid layout

Simple program which demonstrates Swing/AWT Grid layout: This post is intended to the beginners of swing/awt. This below program creates a JFrame with 2 labels, 2 textfields and 2 buttons. import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; /**  * @author Nagasharath Kopalle  *  * Simple program to demontrate Gridlayout  *  */ public class GridLayoutDemo extends JFrame { private JLabel labelName = null; private JTextField textFieldName = null; private JLabel labelPassword = null; private JPasswordField passwordFieldPassword = null; private JButton buttonSubmit = null; private JButton buttonCancel = null; /** * */ public GridLayoutDemo() { initComponents(); } /** * instantiates components and adds properties. */ void initComponents() { this.setTitle("Grid Layout Dem...

Eclipse Java Editor Shortcuts

Eclipse Oxygen Java editor Shortcuts helps in editing source code more comfortably. Frequently used default shortcuts are listed below.  Description Shortcut Delete Line CTRL + D Copy Line CTRL + ALT + Down Arrow Add method level, class level, member level Comments SHIFT + ALT + J Format Java Source code CTRL + SHIFT + F Make line commented CTRL + / Make multiple lines [block] commented   CTRL + SHIFT + / Search selected similar word within the source document CTRL + K Select line from beginning to end SHIFT + End key Select line from end to beginning SHIFT + Home key Select Word by word in same line CTRL + SHIFT + Right Arrow Move line above ALT + Up Arrow Move line below ALT + Down Arrow Shortcuts can ...