by Dinesh Thakur

When more than one thread has to use a shared resource, Java finds a way of ensuring that only one thread uses the resources at one point of time; this is called synchronization.

 
by Dinesh Thakur

A thread can undergo some states during its life cycle. It is because in a multithreaded environment when multiple threads are executing only one thread can use the CPU at a time, and all other threads should be in some other states either waiting for their turn for the CPU or waiting for some other condition to be satisfied.

A thread is always in one of five states: newborn, runnable, running, dead and blocked. Figure shows the life cycle of a thread.

 
by Dinesh Thakur

Java Thread: One of the dominant features of the Java language is that it provides built-in support for multithreading - the concurrent running of multiple threads within the same program. Creating a thread in Java is relatively easy. Unlike the old fashioned programming languages, where you have to invoke system-dependent procedures and functions to implement multithreading, in Java, it is no harder than creating an instance of other classes.

The programs that we have written so far had only one entry point (main() method) and one exit point. All the instructions in these programs executed serially one at a time beginning at the first statement in main () and continuing sequentially through the statements until the program ends. This single sequence of executable statements within a program is known as a thread. In other words, a thread is s flow of execution of a task in a program, so it is known as a thread of execution or a thread of control.

 
by Dinesh Thakur

Every thread in Java has a priority that helps the thread scheduler to determine the order in which threads scheduled. The threads with higher priority will usually run before and more frequently than lower priority threads. By default, all the threads had the same priority, i.e., they regarded as being equally distinguished by the scheduler, when a thread created it inherits its priority from the thread that created it. However, you can explicitly set a thread's priority at any time after its creation by calling its setPriority() method. This method accepts an argument of type int that defines the new priority of the thread. Its syntax is.

 
by Dinesh Thakur

The Java language and its run-time system was designed keeping in mind about multithreading. The run-time system depend upon multithreading. Java provides asynchronous thread environment, this helps to increase the utilization of CPU.

 
by Dinesh Thakur

The ability of the Operating system to execute several programs simultaneously is known as multitasking. In system terminology, it is is a powerful programming tool that makes it possible to achieve concurrent execution of multiple units of a program called multithreading. In multithreading,  the application (process) is divided into two or more subprograms (processes), Several such processes originating from a single task, can be simultaneously started and handled by Java, which can be implemented at the same time in parallel. The processor is doing only one thing at a time, but it switches between the processes so fast that it appears to human beings that all of them are being done simultaneously. This mechanism of treating a single task as several independent processes simultaneously is called multithreading. Each separate process is called a thread. Each thread is executed one at a time in the CPU. Multithreading enables a program to do more than one task at a time and also to synchronize these tasks.  

 
by Dinesh Thakur

Exception handling is a technique of processing problems that occur during the execution of the program. Using exception handling, we can test the code and avoid it from exiting abruptly.

Exception handling in Java accomplished by using five keywords: try, catch, throw, throws and finally.

 
by Dinesh Thakur

In a class hierarchy, A subclass can contain a method with the same signature and return type as in its superclass, then the method in the subclass is said to override the method in the superclass. However in certain situations, the subclass need to modify the implementation (code) of a method defined in the superclass without changing the parameter list. This is achieved by overriding or redefining the method in the subclass.

 
by Dinesh Thakur

Multiple inheritance Occurs when a subclass has more than one superclass, whereby it inherits features from all superclasses. Some OOP languages support this, but Java does not. Java only supports single inheritance. The use of multiple inheritances makes the programming language for more complex to use, learn and implement. The designers of Java wanted to make the language simpler and free from any complication and ambiguity. So the Java designers decided they could do without it. But while working with programs in Java, we often come across situations where we want multiple inheritances.

 
by Dinesh Thakur

Java platform provides you to define a class within another class. Such a class is known as nested class. The class that holds the nested class is known as Outer class. It is merely a convenient way of grouping two or more classes together into a single unit or module. A class that contains a nested class is known as the enclosing or outer class of the nested class. In java, We can't use the static keyword with a class unless it is an Inner class(or non-static nested class), but we can make a nested class static in Java.



 
by Dinesh Thakur

All the classes in Java that you have defined so far are subclasses by default whether or not you have specified the superclass. The class which all classes in Java are descendent of (directly or indirectly) is java.lang.Object class. So each class inherits the instance methods of Object class. It is important to be familiar with the methods provided by the Object class so that you can use them in your class.



 
by Dinesh Thakur

Sometimes you need a pure class, and to be instantiated only once in your code. When this is the case, you can use an anonymous inner class. It is called an inner class because it defined inside another class.

Anonymous inner class are the local inner classes that declared without a name, but an object of this class can be created. All of the code for the anonymous class is coded within the method where we need to create an instance of the anonymous class. Since anonymous inner classes do not have a name so you cannot use the new keyword in the usual way to create an instance of the class. Anonymous inner classes are declared and instantiated at the same time.

 
by Dinesh Thakur

Abstraction in Java: The words "data abstraction" and "information hiding" are generally used interchangeably. The two terms mean the same thing in this context. Abstraction is a simple representation of a complicated situation. It is a technique where we hide irrelevant details and represent only the essential aspects of a context so that one can focus on features one is interested; It helps to deal a complex system by concentrating on the essential features only. It is designed to make it easier to maintain, read and work on the code. In the object-oriented model, a class is an abstraction of existing entities in the domain of the software system. Ex: A car viewed as a car rather than its components.



 
by Dinesh Thakur

An abstract class is one whose header contains the reserved keyword, abstract. An abstract class is distinguishable from other classes by the fact that it is not possible to use the new operator to construct objects from them directly. Each abstract class may have at least zero abstract methods.

Some time there is a situation in which you feel the need of a superclass that has only declaration of few or all methods, with definition of few or none methods. But it is necessary that it must not be a completely defined or implemented class. The methods are declared only and specified by abstract type modifier called abstract method.

 
by Dinesh Thakur

Association is nothing more than  a connection that exists between two classes.  We use the objects of these classes to set up the connection. Association can be  done in three ways one-to-one, one-to-many and many-to-many. Let’s have a look at an example showing how association is implemented in Java.

 
by Dinesh Thakur

The super() keyword is used to reference objects for the immediate parent class. A subclass inherits the accessible data fields and methods from its superclass, but the constructors of the superclass are not inherited in the subclass. They can only be invoked from constructors of the subclass( es) using the keyword super.

 
by Dinesh Thakur

One of the critical aims of Java and other object-oriented programming languages is that reusable classes should be created and made available to programmers so that the same code is not written repeatedly by different people. We reused the classes within a program by extending the classes and implementing the interfaces. Moreover, most of the classes that we created so far did not interact much with the outside world as they were almost self-contained, except that some of them referred to Java classes such as String from java.lang package and Scanner from java.util.lang package. However, while designing most of the real world applications, the developers write code in the form of Java classes, which eventually needs to be brought together to work as a single application.



 
by Dinesh Thakur

Java applet inherits features from the class Applet. Thus, whenever an applet is created, it undergoes a series of changes from initialization to destruction. Various stages of an applet life cycle are depicted in the figure below:

 
by Dinesh Thakur

The listener class that implements the Listener interface must provide bodies for all of the methods of that interface. It is not a problem for all the semantic listener interfaces such as ActionEvent, ItemEvent, TextEvent, AdapterEvent as each of them declares only one method. However, for all the low-level listener interfaces where each interface contains multiple methods, implementing each method can be somewhat tedious, especially when we have to define methods in which we are not interested. For example: Suppose we are interested in setting up only one listener interface method windowClosing() of the WindowListener interface that causes the program to terminate. In that case, we would not only need to provide code for windowClosing() method but also need to write empty bodies for the other methods available in the WindowListener interface.

 
by Dinesh Thakur

A Java method is a collection of statements, all performing a particular operation.  Let’s say that you were calling the method called System.out.println(); Java executes some different statements that print a message to your console.  We’re going to look at the creation of methods or, as they often called, functions.

 

Page 6 of 9

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.