RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
Multi Threaded Programming :
Threads allows a program to operate more efficiently by doing multiple things at the same time.
Threads can be used to perform complicated tasks in the background without interrupting the main
program.
Typically, we can define threads as a subprocess with lightweight with the smallest unit of
processes and also has separate paths of execution. The main advantage of multiple threads is
efficiency (allowing multiple things at the same time. For example, in MS Word. one thread
automatically formats the document while another thread is taking user input. Another advantage is
quick response, if we use multiple threads in a process and if a thread gets stuck due to lack of
resources or an exception, the other threads can continue to execution, allowing the process (which
represents an application) to continue to be responsive.
Why Threads are used?
Now, we can understand why threads are being used as they had the advantage of being lightweight
and can provide communication between multiple threads at a Low Cost contributing to effective
multi-tasking within a shared memory environment.
Life Cycle Of Thread
There are different states Thread transfers into during its lifetime, let us know about those states in
the following lines: in its lifetime, a thread undergoes the following states, namely:
• New State
• Active State
• Waiting/Blocked State
• Timed Waiting State
• Terminated State
How to Create Threads using Java Programming Language?
We can create Threads in java using two ways, namely :
• Extending Thread Class
• Implementing a Runnable interface
1|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
1)Java Thread Example by extending Thread class
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
2) Java Thread Example by implementing Runnable interface
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
t1.start();
}
}
2|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
AWT :
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or
windows-based applications in Java.
Java AWT components are platform-dependent i.e. components are displayed according to the view
of operating system. AWT is heavy weight i.e. its components are using the resources of underlying
operating system (OS).
The java.awt package provides classes for AWT API such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
Why AWT is platform independent?
Java AWT calls the native platform calls the native platform (operating systems) subroutine for
creating API components like TextField, ChechBox, button, etc.
For example, an AWT GUI with components like TextField, label and button will have different
look and feel for the different platforms like Windows, MAC OS, and Unix. The reason for this is
the platforms have different view for their native components and AWT directly calls the native
subroutine that creates those components.
In simple words, an AWT application will look like a windows application in Windows OS whereas
it will look like a Mac application in the MAC OS.
3|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
Java AWT Hierarchy:
Types of Containers in Java AWT:
There are four types of containers in Java AWT.
1. Window: Window is a top-level container that represents a graphical window or dialog box.
The Window class extends the Container class, which means it can contain other
components, such as buttons, labels, and text fields.
2. Panel: Panel is a container class in Java. It is a lightweight container that can be used for
grouping other components together within a window or a frame.
3. Frame: The Frame is the container that contains the title bar and border and can have menu
bars.
4. Dialog: A dialog box is a temporary window an application creates to retrieve user input.
4|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
Example:
import java.awt.*;
public class AwtProgram1 {
public AwtProgram1()
{
Frame f = new Frame();
Button btn=new Button("Hello World");
btn.setBounds(80, 80, 100, 50);
f.add(btn); //adding a new Button.
f.setSize(300, 250); //setting size.
f.setTitle("Welcome"); //setting title.
f.setLayout(null); //set default layout for frame.
f.setVisible(true); //set frame visibility true.
}
public static void main(String[] args) {
AwtProgram1 awt = new AwtProgram1(); //creating a frame.
}
}
5|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
Event Handling :
An event can be defined as changing the state of an object or behavior by performing actions.
Actions can be a button click, cursor movement, keypress through keyboard or page scrolling, etc.
The java.awt.event package can be used to provide various event classes.
Classification of Events
• Foreground Events
• Background Events
1. Foreground Events
Foreground events are the events that require user interaction to generate, i.e., foreground events are
generated due to interaction by the user on components in Graphic User Interface (GUI).
Interactions are nothing but clicking on a button, scrolling the scroll bar, cursor moments, etc.
2. Background Events
Events that don’t require interactions of users to generate are known as background events.
Examples of these events are operating system failures/interrupts, operation completion, etc.
Swing :
Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based
applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written
in java.
Unlike AWT, Java Swing provides platform-independent and lightweight components.
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
6|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit IV
No. Java AWT Java Swing
1) AWT components are platform- Java swing components are platform-
dependent. independent.
2) AWT components are heavyweight. Swing components are lightweight.
3) AWT doesn't support pluggable look Swing supports pluggable look and feel.
and feel.
4) AWT provides less components than Swing provides more powerful
Swing. components such as tables, lists,
scrollpanes, colorchooser, tabbedpane etc.
5) AWT doesn't follows MVC(Model View Swing follows MVC.
Controller) where model represents
data, view represents presentation and
controller acts as an interface between
model and view.
Database Handling using JDBC :
JDBC is an acronym for Java Database Connectivity. It’s an advancement for ODBC ( Open
Database Connectivity ). JDBC is a standard API specification developed in order to move data
from the front end to the back end. This API consists of classes and interfaces written in Java. It
basically acts as an interface (not the one we use in Java) or channel between your Java program
and databases i.e it establishes a link between the two so that a programmer can send data from Java
code and store it in the database for future use.
Steps to Connect Java Application with Database
Below are the steps that explains how to connect to Database in Java:
Step 1 – Import the Packages
Step 2 – Load the drivers using the forName() method
Step 3 – Register the drivers using DriverManager
Step 4 – Establish a connection using the Connection class object
Step 5 – Create a statement
Step 6 – Execute the query
Step 7 – Close the connections
7|Page
Study Notes (For Private Circular Only)