Java Packages and Multithreading Notes
Packages
Definition
Packages are the group of similar classes, interfaces and similar types of functions. It acts as a container for
classes and packages.
Advantages of Packages
1. The classes present in the package of other programs can be easily reused.
2. Classes can be uniquely compared with classes in other packages.
3. They may be referred fully with package name and class name.
4. It also provides a way of hiding classes.
5. Packages make the program organized.
Java API Packages
Java API provides a large number of classes grouped into different packages.
1. [Link]
It is a language package where the classes are used by Java compiler itself. Import is not required.
It includes:
• Primitive data types
• String
• Math functions
• Threads
• Exceptions
Example:
1
[Link]();
2. [Link]
It has utility classes such as:
• Scanner
• Vector
• Date
• Collections
Example:
Scanner sc = new Scanner([Link]);
3. [Link]
It includes input and output classes.
Examples:
• BufferedReader
• Console
• InputStream
• OutputStream
4. [Link]
It contains graphical user interface classes.
Examples:
• Button
• TextArea
• Menus
• Color
• Graphics
• Font
2
5. [Link]
It contains all networking classes.
Examples:
• Socket
• URL
• Network adapter
6. [Link]
It contains classes for creating applets and GUI for web pages.
Accessing Packages
1. Using Fully Qualified Name
Example:
[Link] sc = new [Link]([Link]);
2. Using Import Statement
Example:
import [Link].*;
Creating Packages
Step 1
Declare the package name at the beginning of the file.
Syntax:
3
package packageName;
Example:
package [Link];
Step 2
Define the class and declare it public.
package [Link];
public class Calculator {
public double add(double a, double b) {
return a + b;
}
public double subtract(double a, double b) {
return a - b;
}
public double multiply(double a, double b) {
return a * b;
}
}
Directory Structure
JavaPackage/
│
├── src/
│ └── com/
│ └── college/
│ └── math/
│ └── [Link]
│
├── [Link]
4
│
└── bin/
Important Points
1. The subdirectory name must match the package name.
2. The package keyword must be written in the first statement of Java source file.
3. A Java package file can contain more than one class definition.
4. Only one class may be declared public.
5. Public class name must match the Java file name.
Compile the Package Class
javac -d bin src/com/college/math/[Link]
It compiles the Java file and stores the class file inside the bin folder.
Write Main Class and Import the Package
import [Link];
public class Main {
public static void main(String[] args) {
Calculator pkg = new Calculator();
double sum = [Link](10.5, 4.5);
double diff = [Link](20, 8);
double mult = [Link](3, 7);
[Link](sum);
[Link](diff);
[Link](mult);
}
}
5
Compile Main Class
javac -cp bin -d bin [Link]
-cp bin tells Java compiler to look in bin folder for package classes.
Run the Program
java -cp bin Main
---
Multithreading
Definition
Multithreading is a process where a program is divided into various small processes called threads.
A thread is the smallest unit of a process which runs simultaneously in parallel process.
Process
A process is an active entity running in operating system to complete a task.
Thread
A thread is the smallest unit of a process that has a single flow of control.
Advantages of Multithreading
1. Gives quick response for multiple programs.
2. More than one task can be done at the same time.
3. Improves performance and efficiency.
4. Better utilization of CPU.
6
5. Supports multitasking.
Multitasking
1. Process Based Multitasking
Multiple programs run at the same time.
Example:
• Music player and browser running together.
2. Thread Based Multitasking
Multiple threads run inside the same program.
Example:
• Typing and spell checking in MS Word.
Life Cycle of a Thread
A thread includes the following states:
1. New
2. Runnable
3. Running
4. Blocked / Waiting
5. Dead State
1. New State
When a thread object is created but not started.
Example:
Thread t = new Thread();
7
2. Runnable State
The thread is ready for execution and waiting for CPU.
Example:
[Link]();
3. Running State
The thread is executing.
4. Blocked / Waiting State
The thread waits for another thread or resource.
5. Dead State
After execution of thread is completed or thread is stopped.
Thread Life Cycle Diagram
New
|
start()
|
Runnable
|
Running
/
Waiting Dead
8
Summary
• Packages are used to organize Java classes.
• Java provides many built-in API packages.
• Packages can be imported and reused.
• Multithreading allows multiple tasks to run simultaneously.
• Threads improve program performance.