UNIT 3
1)What is Method overloading?
In Java, Method Overloading allows us to define multiple methods with the same
name but different parameters within a class. This difference may include:
The number of parameters
The types of parameters
The order of parameters
Method overloading in Java is also known as Compile-time Polymorphism,
Static Polymorphism, or Early binding, because the decision about which
method to call is made at compile time. When there are overloaded methods that
accept both a parent type and a child type, and the provided argument could
match either one, Java prefers the method that takes the more specific (child)
type. This is because it offers a better match.
Key features of Method Overloading
Multiple methods can share the same name in a class when their
parameter lists are different.
Overloading is a way to increase flexibility and improve the readability
of code.
Overloading does not depend on the return type of the method, two
methods cannot be overloaded by just changing the return type.
Example program
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum()
// This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum()
// This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum()
// This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Output
30
60
31.0
2)Explain about Static Members?
In Java, static members are elements of a class that belong to the class itself, rather than to any
specific instance (object) of that class. They are declared using the static keyword. Types of
Static Members:
Static Variables (Class Variables):
o Declared with the static keyword.
o A single copy of a static variable is shared among all instances of the
class.
o Changes to a static variable made through one instance are reflected in all
other instances.
o Accessed directly using the class name, e.g., [Link].
Java
class MyClass {
static int count = 0; // Static variable
// ...
}
Static Methods:
o Declared with the static keyword.
o Can be called directly using the class name without creating an object,
e.g., [Link]().
o Cannot access non-static (instance) variables or call non-static methods
directly, as they do not operate on a specific object. They can only access other static
members.
Java
class MyClass {
static void displayMessage() {
[Link]("This is a static method.");
}
// ...
}
Static Blocks:
o Used to initialize static variables or perform static setup tasks.
o Executed automatically when the class is loaded into memory, before any
objects are created or any static methods are called.
o A class can have multiple static blocks, which are executed in the order
they appear in the code.
Java
class MyClass {
static {
[Link]("Static block executed.");
// Initialize static variables here
}
// ...
}
Static Nested Classes:
o A nested class declared with the static keyword.
o Can be instantiated without creating an instance of the outer class.
o Can only access static members of the outer class directly.
Java
class OuterClass {
static class StaticNestedClass {
// ...
}
}
Key Characteristics of Static Members:
Class-level scope: They belong to the class, not to individual objects.
Single copy: Only one copy of a static variable exists, shared by all instances.
Direct access: Accessed using the class name, eliminating the need for object
creation.
Memory management: Can be used for efficient memory usage by sharing
common data or functionality across all instances.
3) Define Nesting of methods?
Nesting of methods is the practice of calling one method from within another method of the same class,
allowing for a hierarchical structure where methods can invoke each other to perform related tasks.
While the term "nested method" often implies a method defined inside another method, which is more
common in languages like C# and Pascal, in Java, it generally refers to calling one method by its
name directly from within another method of the same class.
How it Works (in Java)
In Java, you can achieve method nesting by calling a method directly by its name from within
another method within the same class.
1. Method Definition: You define multiple methods within a single class, such
as volume() , area() , and perimeter() .
2. Method Invocation: A method, like volume() , can call another method, such
as area() , by its name.
3. Hierarchical Execution: The method being called ( area() ) executes, and then
the control returns to the calling method ( volume() ), and so on.
Example program
public class MethodNestingExample {
public void outerMethod() {
[Link]("Inside outerMethod");
innerMethod(); // Calling innerMethod
}
public void innerMethod() {
[Link]("Inside innerMethod");
}
public static void main(String[] args) {
MethodNestingExample obj = new MethodNestingExample();
[Link]();
}
}
Output
Inside outerMethod
Inside innerMethod
In this example, outerMethod() calls innerMethod() by its name.
4)What is ‘this’ keyword in java?
In Java, "this" is a reference variable that refers to the current object, or can be
said "this" in Java is a keyword that refers to the current object instance. It is
mainly used to,
Call current class methods and fields
To pass an instance of the current class as a parameter
To differentiate between the local and instance variables.
Using "this" reference can improve code readability and reduce naming conflicts.
The this keyword can be used to access instance variables and methods of the
object on which the method or constructor is being invoked.
Below is the implementation of "this" reference:
// Java program to demonstrate
// this reference
// Driver Class
public class Person {
// Fields Declared
String name;
int age;
// Constructor
Person(String name, int age)
{
[Link] = name;
[Link] = age;
}
// Getter for name
public String get_name() { return name; }
// Setter for name
public void change_name(String name)
{
[Link] = name;
}
// Method to Print the Details of
// the person
public void printDetails()
{
[Link]("Name: " + [Link]);
[Link]("Age: " + [Link]);
[Link]();
}
// main function
public static void main(String[] args)
{
// Objects Declared
Person first = new Person("ABC", 18);
Person second = new Person("XYZ", 22);
[Link]();
[Link]();
first.change_name("PQR");
[Link]("Name has been changed to: "
+ first.get_name());
}
}
Output
Name: ABC
Age: 18
Name: XYZ
Age: 22
Name has been changed to: PQR
Explanation:
Here, we have defined a "Person" class with two private fields "name"
and "age". We have defined the Person class constructor to initialize these fields
using "this" keyword. We have also defined "getter and setter" methods for these
fields which use this keyword to refer to the current object instance.
In the printDetails() method, we have used "this" keyword to refer to
the current object instance and print its name, age, and object reference.
In the Main class, we have created two Person objects and called the
printDetails() method on each object. The output shows the name, age, and object
reference of each object instance.
Methods to Use "this" in Java
Following are the ways to use the "this" keyword in Java mentioned below:
1. Using "this" keyword to refer to current class instance variables.
2. Using this() to invoke the current class constructor
3. Using 'this' keyword to return the current class instance
4. Using 'this' keyword as the method parameter
5. Using 'this' keyword to invoke the current class method
6. Using 'this' keyword as an argument in the constructor call
5)What is Method overriding?
Overriding in Java occurs when a subclass or child class implements a
method that is already defined in the superclass or base class. When a
subclass provides its own version of a method that is already defined in its
superclass, we call it method overriding. The subclass method must match the
parent class method's name, parameters, and return type.
Rules for Overriding:
Name, parameters, and return type must match the parent method.
Java picks which method to run, based on the actual object type, not
just the variable type.
Static methods cannot be overridden.
The @Override annotation catches mistakes like typos in method
names.
Example: In the code below, Dog overrides the move() method from Animal but
keeps eat() as it is. When we call move() on a Dog object, it runs the dog-specific
version.
// Example of Overriding in Java
class Animal {
// Base class
void move() { [Link](
"Animal is moving."); }
void eat() { [Link](
"Animal is eating."); }
}
class Dog extends Animal {
@Override void move()
{ // move method from Base class is overriden in this
// method
[Link]("Dog is running.");
}
void bark() { [Link]("Dog is barking."); }
}
public class Geeks {
public static void main(String[] args)
{
Dog d = new Dog();
[Link](); // Output: Dog is running.
[Link](); // Output: Animal is eating.
[Link](); // Output: Dog is barking.
}
}
Output
Dog is running.
Animal is eating.
Dog is barking.
Explanation: The Animal class defines base functionalities like move() and eat(). The Dog
class inherits from Animal and overrides the move() method to provide a specific
behavior Dog is running. Both classes can access their own methods. When creating a Dog
object, calling move() executes the overridden method.
6)What are final Variables and Methods?
The final keyword in Java is a non-access modifier used to restrict the modification or
extension of variables, methods, and classes.
Final Variables:
A final variable, once initialized, cannot have its value changed.
It can be initialized either at the time of declaration or within the constructor of the
class.
Attempting to reassign a value to a final variable after its initial assignment will result in
a compile-time error.
final variables are often used to define constants.
While not strictly required, a common convention for final variable names is to use all
uppercase letters, often in conjunction with the static keyword for class-level
constants.
Example Program
public class Constants {
public static final int MAX_VALUE = 100; // Initialized at declaration
private final String name; // Blank final variable
public Constants(String name) {
[Link] = name; // Initialized in the constructor
}
}
Final Methods:
A final method cannot be overridden by any subclass.
This ensures that the implementation of the method remains consistent and cannot be
altered by inheriting classes.
final methods are useful for protecting critical logic or ensuring specific behavior within
a class hierarchy.
Example Program
public class Parent {
public final void importantMethod() {
[Link]("This method cannot be overridden.");
}
}
public class Child extends Parent {
// public void importantMethod() { // This would cause a compile-time error
// [Link]("Attempting to override.");
// }
}
7)Define Final Classes?
Introduction to Final Class in Java
As the name suggests, the Final Class in Java uses the final keyword for its
declaration. The final keyword in Java restricts the user; similarly, the final class
means that the class cannot be extended. We can only create a final class if it is
complete, which means it cannot be an abstract class. All wrapper classes in Java
are final classes, such as String, Integer, etc. Any subclass cannot inherit final class,
If we try to inherit a final class, the compiler throws an error during compilation.
How to Create Final Classes?
We can use Java's final keyword to create a final class. The class's definition should
be complete and not abstract.
Syntax:
final class className
{
// Body of class
}
where final is the keyword used to declare the final class and className is the name
of the class we are defining.
Example 1: How to Use Final Class?
We can simply define a final class using the final keyword and can write the class
body code according to our needs.
In the code below, we are creating a final class named myFinalClass, which has a
method called myFinalMethod() that prints some text. So for using our final class, we
are just creating an instance of our final class in our main class; now, we are just
calling our method myFinalMethod() from the class instance, which prints the final
output of our code.
final class myFinalClass
{
void myFinalMethod()
{
[Link]("We are in the final class we just
created");
}
}
class MainClass
{
public static void main(String arg[])
{
myFinalClass fc = new myFinalClass();
[Link]();
}
}
utput:
Simply the print statement present inside myFinalMethod is executed.
We are in the final class we just created
8) What is Visibility Control ?
In Java, visibility control is managed through Access Modifiers, which define the
accessibility of classes, methods, variables, and constructors. These modifiers are
crucial for implementing encapsulation, a core principle of Object-Oriented
Programming (OOP), by restricting access to sensitive parts of your code.
There are four primary access modifiers in Java:
private:
Visibility: Only accessible within the class where it is declared.
Use Case: Used for data and methods that should not be exposed to other classes,
ensuring data integrity and hiding implementation details.
default (Package-Private):
Visibility: Accessible only within the same package. No explicit keyword is used; it's
the default if no access modifier is specified.
Use Case: Useful for classes, methods, or variables that are intended for internal use
within a package but not for external access.
protected:
Visibility: Accessible within the same package and by subclasses (even if they are in
different packages).
Use Case: Primarily used for members that are intended to be inherited and
potentially overridden by subclasses, while still maintaining a level of protection from
unrelated classes.
public:
Visibility: Accessible from anywhere in the program.
Use Case: Used for classes, methods, and variables that are meant to be widely
accessible and part of the public API of a component.
Example:
package [Link].package1;
class MyClassDefault { // Default (package-private) class
private int privateVar = 10; // Private variable
protected String protectedVar = "Hello"; // Protected variable
public void publicMethod() { // Public method
[Link]("Public method called.");
}
void defaultMethod() { // Default (package-private) method
[Link]("Default method called.");
}
}
package [Link].package2;
import [Link]; // Can't directly access
MyClassDefault
class SubClass extends [Link] {
public void accessProtected() {
[Link](protectedVar); // Accessible due to inheritance
}
}
9)How to Access Interface Variables?
Variables declared within a Java interface are implicitly public, static, and final. This
means they are constants, belonging to the interface itself, and their values cannot be
changed after initialization.
To access an interface variable, use the interface name followed by the dot operator
and the variable name.
Example Program
interface MyInterface {
int MAX_VALUE = 100; // public static final by default
}
public class Main {
public static void main(String[] args) {
[Link]("Max value from interface: " + MyInterface.MAX_VALUE);
}
}
Explanation:
interface MyInterface: Defines an interface named MyInterface.
int MAX_VALUE = 100;: Declares a variable MAX_VALUE within the interface. Even
though public static final is not explicitly written, the compiler automatically adds
these modifiers.
MyInterface.MAX_VALUE: In the main method, MAX_VALUE is accessed directly using the
interface name (MyInterface) because it's a static member of the interface.
Key points:
Interface variables must be initialized at the time of declaration as they are final.
Their values cannot be modified by any class implementing the interface.
They are accessed using the interface name, not through an object of an implementing
class, due to their static nature.
10)How to Construct Strings?
In Java, strings are objects representing sequences of characters. There are two
primary ways to create a String object: using string literals.
This is the most common and convenient method. When you enclose a sequence of
characters within double quotes, Java treats it as a String literal. These literals are
stored in the "String Constant Pool," which helps in memory optimization by reusing
existing String objects with the same value.
Java
String myString = "Hello, World!";
using the new keyword.
You can explicitly create a String object using the new keyword and the String class
constructor. This creates a new String object in the heap memory, regardless of
whether an identical String literal already exists in the String Constant Pool.
Java
String anotherString = new String("Java Programming");
Additional Considerations:
Immutability:
Java String objects are immutable, meaning their content cannot be changed after
creation. Any operation that appears to modify a String (e.g., concatenation) actually
creates a new String object.
Constructors:
The String class offers various constructors to initialize strings from different sources,
such as character arrays or byte arrays.
StringBuffer and StringBuilder:
For situations requiring mutable sequences of characters, consider
using StringBuffer (thread-safe) or StringBuilder (not thread-safe, better
performance in single-threaded environments).
11)Define Operating on Strings?
Operations on strings are methods for manipulating sequences of characters,
including concatenation (joining strings), substring extraction (getting parts of a string),
searching for characters or substrings, comparison for equality, length calculation, and
case conversion (like to uppercase or lowercase). These operations are fundamental in
programming and are supported by various methods in languages like Python,
JavaScript, and Java to process and transform textual data.
Common String Operations
Concatenation: Joining two or more strings to create a single, longer string.
o Example: "Hello" + " " + "World" results in "Hello World".
Length Calculation: Determining the number of characters in a string.
Substring Extraction: Retrieving a portion of a string.
Searching: Finding the position of a specific character or substring within a string.
Comparison: Checking if two strings are identical.
Case Conversion: Changing a string's case, such as converting all characters to
uppercase or lowercase.
Insertion/Deletion: Adding or removing characters or substrings at specific positions.
Splitting: Dividing a string into a list of smaller strings based on a delimiter.
Examples in Python
Concatenation: s1 + s2
Repetition: s * n (repeats the string s n times)
Substring: s[start:stop:step] (extracts a portion of the string)
Length: len(s)
Case Change: [Link](), [Link]()
Splitting: [Link](delimiter)
12)What is Arrays of Strings?
A String Array in Java is an array that stores string values. The string is
nothing but an object representing a sequence of char values. Strings are
immutable in Java, this means their values cannot be modified once created.
When we create an array of type String in Java, it is called a String Array in
Java. In this article, we will learn the concepts of String Arrays in
Java including declaration, initialization, iteration, searching, sorting,
and converting a String Array to a single string.
Declaration of String Arrays
A String array can be declared with or without specifying its size. Below is
the example:
String[] str0; // declaration without size String[] str1 = new String[4];
//declaration with size
In the above example,
str0 is declared without specifying its size.
str1 is declared with a size of 4.
We can use both of these ways to declare String array in Java.
Initialization of String Arrays
Declaring and Initializing Together
In this method, we are declaring the values at the same line.
String[] arr0 = new String[]{"Apple", "Banana", "Orange"};
Using Short Form
This method is the short form of the first method i.e. initialize the array at the
time of declaration.
String[] arr1={"Apple","Banana","Orange"};
Declaring First, Initializing Later
In this method, we are declaring the String array with size first and after that
we are storing data into it.
String[] arr2=new String[3];
arr2[0]="Apple";
arr2[1]="Banana";
arr2[2]="Orange";
Iterating Over String Arrays
To iterate through a String array we can use a looping statement. So
generally we have three ways to iterate over a string array.
The first method is to use a for-each loop.
The second method is using a simple for loop.
And the third method is to use a while loop.
Example Program
// Java program to demonstrate various
// methods to iterate over a string array
public class GFG {
public static void main(String[] args) {
String[] arr = { "Apple", "Banana", "Orange" };
// First method
for (String i : arr) {
[Link](i + " ");
}
[Link]();
// Second method
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
// Third method
int i = 0;
while (i < [Link]) {
[Link](arr[i] + " ");
i++;
}
[Link]();
}
}
Output
Apple Banana Orange
Apple Banana Orange
Apple Banana Orange
Unit 4
1) What are java API packages?
Java API packages are collections of related classes and interfaces that provide pre-
built functionalities for various programming tasks. These packages are part of the Java
Development Environment and are categorized into built-in packages and user-defined
packages.
Built-in Packages (Java API):
These are the core packages provided by the Java platform, offering a vast library of
reusable components. Some prominent examples include:
[Link]:
Contains fundamental classes for the Java language, such
as Object, String, System, Math, and wrapper classes for primitive data types. This
package is automatically imported into every Java program.
[Link]:
Provides classes for input and output operations, including file handling, streams, and
serialization. Examples include FileInputStream, FileOutputStream, BufferedReader,
and PrintWriter.
[Link]:
Offers utility classes for various purposes, including data structures
(e.g., ArrayList, HashMap), date and time manipulation (Date, Calendar), and regular
expressions.
[Link]:
Contains classes for network programming, enabling communication over networks
using sockets, URLs, and other network-related functionalities.
[Link] (Abstract Window Toolkit):
Provides classes for creating graphical user interfaces (GUIs), including components
like buttons, text fields, and windows.
[Link]:
An extension of AWT, offering a more robust and platform-independent set of GUI
components.
[Link]:
Provides classes for connecting to and interacting with relational databases using
Java Database Connectivity (JDBC).
User-defined Packages:
Developers can create their own packages to organize their code into logical units,
enhance modularity, and prevent naming conflicts. This involves using
the package keyword at the beginning of a Java source file to declare the package to
which the class belongs.
Advantages of Java Packages:
Organization:
Grouping related classes and interfaces improves code structure and maintainability.
Access Protection:
Packages provide a mechanism for controlling access to classes and members within
a package.
Naming Collision Prevention:
Packages help avoid conflicts when multiple classes or interfaces have the same
name by providing a unique namespace.
Reusability:
Built-in and user-defined packages promote code reuse by making classes and
functionalities readily available for use in different parts of an application or across
multiple projects.
2)How to Define a Package?
In Java, a package is a mechanism for organizing related classes and interfaces into a
single namespace. It helps in managing code, preventing naming conflicts, and
controlling access to classes and members.
Defining a Package:
Package Declaration: The package statement must be the first non-comment, non-
whitespace line in a Java source file. It declares the package to which the classes and
interfaces within that file belong.
package [Link]; // Example package declaration
Naming Convention: Package names are typically written in lowercase and use
reverse domain name notation (e.g., [Link]). This convention helps
ensure uniqueness and avoids naming clashes.
Directory Structure: The package declaration directly corresponds to the directory
structure on your file system. If you declare a package as [Link], your
source file (e.g., [Link]) must reside in a directory structure
like com/example/myapp/[Link], relative to a base directory that is part of your
classpath.
o For example, if your base directory is src, the [Link] file would be located
at src/com/example/myapp/[Link].
Example:
Let's say you want to create a package named myutilities and a
class StringUtils within it. Create the directory structure.
myproject/
└── src/
└── myutilities/
└── [Link]
Define [Link].
// [Link]
package myutilities;
public class StringUtils {
public static String capitalize(String text) {
if (text == null || [Link]()) {
return text;
}
return [Link]([Link](0)) + [Link](1);
}
}
Using Classes from a Package:
To use classes from a defined package in another class, you can: Import the specific
class.
import [Link];
public class Main {
public static void main(String[] args) {
String capitalizedText = [Link]("hello");
[Link](capitalizedText); // Output: Hello
}
}
Import all classes from a package (wildcard import):
import myutilities.*; // Imports all classes in the myutilities package
public class Main {
public static void main(String[] args) {
String capitalizedText = [Link]("world");
[Link](capitalizedText); // Output: World
}
}
Use the fully qualified name.
public class Main {
public static void main(String[] args) {
String capitalizedText = [Link]("java");
[Link](capitalizedText); // Output: Java
}
}
3)What are System Packages?
In Java, the term "system package" often refers to the core built-in packages that are
part of the Java Development Kit (JDK) and provide fundamental functionalities. These
packages are essential for almost any Java application.
The most prominent example of a "system package" is [Link]. This package is
automatically imported into every Java program, meaning you do not need to explicitly
use an import statement for its classes. It contains fundamental classes like:
Object: The root of the class hierarchy.
String: Represents sequences of characters.
System:Provides access to system resources, including standard input/output streams
([Link], [Link], [Link]).
Math: Provides mathematical functions.
Thread: For handling multithreading.
Wrapper classes: For primitive data types (e.g., Integer, Double, Boolean).
Beyond [Link], other commonly used built-in "system packages" include:
[Link]: Contains utility classes like Scanner, ArrayList, HashMap, Date, Random, etc.
[Link]: Provides classes for input and output operations, such
as FileInputStream, FileOutputStream, BufferedReader, PrintWriter, etc.
[Link]: Contains classes for networking, including Socket, URL, ServerSocket, etc.
[Link]: Provides classes for database connectivity (JDBC).
[Link]: and [Link]: Provide classes for creating graphical user interfaces
(GUIs).
These built-in packages are crucial for developing a wide range of Java applications,
from simple command-line tools to complex enterprise systems. They are designed to
be platform-independent and provide a consistent way to interact with underlying
system resources.
4) what are Naming Conventions of Packages ?
Java package naming conventions are designed to ensure uniqueness and organization
within the Java ecosystem. The primary guidelines are:
All Lowercase: Package names should be written entirely in lowercase letters to avoid
conflicts with class or interface names.
// Correct
package [Link];
// Incorrect (due to uppercase letters)
// package [Link];
Reversed Internet Domain Name: Begin package names with the reversed Internet
domain name of the organization to ensure global uniqueness. For example, if a
company's domain is [Link], their packages would start with [Link].
package [Link];
Concatenated Words (No Underscores): When using multiple words in a package
name component, concatenate them directly without using underscores or camel case.
// Correct
package [Link];
// Incorrect (due to underscore or camel case)
// package [Link].deep_space;
// package [Link];
Hierarchical Structure: Subsequent components of the package name can reflect the
internal structure of the organization or project, such as division, department, project, or
module names. Each component should be a valid Java identifier.
package [Link];
5) How do you Create and Access package members?
reating a Package in Java
Creating a package in Java involves organizing related classes, interfaces,
enumerations, and annotation types into a named group. This promotes modularity and
avoids naming conflicts.
Choose a Package Name:
Select a meaningful and unique name, typically following a reverse domain name
convention (e.g., [Link] ).
Create Directory Structure:
Create a directory structure on your file system that mirrors the package
name. For [Link] , you would
have com/example/myproject directories.
Declare the Package:
At the very beginning of each Java source file ( .java) belonging to the package,
include the package statement:
package [Link];
Place Java Files:
Place the corresponding Java source files within the appropriate directory in the
package structure.
Compile:
Compile your Java code, ensuring the compiler can locate the package structure. For
example, using javac from the parent directory of com:
javac com/example/myproject/*.java
Member Access in Java Packages
Access to members (fields and methods) within and across packages is controlled by
access modifiers:
public:
Members are accessible from anywhere, including other packages and classes.
protected:
Members are accessible within the same package and by subclasses, even if the
subclasses are in different packages.
default (package-private):
If no access modifier is specified, members are accessible only within the same
package.
private:
Members are accessible only within the class where they are declared.
Accessing Package Members:
Same Package:
Classes within the same package can directly access public, protected,
and default members of other classes in that package.
Different Package:
public members: Can be accessed directly using the class name or an object
instance.
protected members: Can only be accessed by subclasses of the class, or by classes
within the same package as the member.
default and private members: Cannot be accessed from a different package.
Using import Statements:
To use classes from other packages without using their fully qualified names every time,
you can use the import statement:
import [Link]; // Imports a specific class
import [Link].*; // Imports all classes from a package
6) What is Thread Scheduling?
There is a component in Java that basically decides which thread should
execute or get a resource in the operating system.
Scheduling of threads involves two boundary scheduling.
1. Scheduling of user-level threads (ULT) to kernel-level threads (KLT) via
lightweight process (LWP) by the application developer.
2. Scheduling of kernel-level threads by the system scheduler to perform
different unique OS functions.
Lightweight Process (LWP)
Light-weight process are threads in the user space that acts as an interface for
the ULT to access the physical CPU resources. Thread library schedules which
thread of a process to run on which LWP and how long. The number of LWPs
created by the thread library depends on the type of application. In the case of
an I/O bound application, the number of LWPs depends on the number of user-
level threads. This is because when an LWP is blocked on an I/O operation,
then to invoke the other ULT the thread library needs to create and schedule
another LWP. Thus, in an I/O bound application, the number of LWP is equal
to the number of the ULT. In the case of a CPU-bound application, it depends
only on the application. Each LWP is attached to a separate kernel-level
thread.
int Pthread_attr_setscope(pthread_attr_t *attr, int scope)
The first parameter denotes to which thread within the process the scope is
defined.
The second parameter defines the scope of contention for the thread
pointed. It takes two values.
PTHREAD_SCOPE_SYSTEM
PTHREAD_SCOPE_PROCESS
If the scope value specified is not supported by the system, then the function
returns ENOTSUP.
Allocation Domain
The allocation domain is a set of one or more resources for which a thread
is competing. In a multicore system, there may be one or more allocation
domains where each consists of one or more cores. One ULT can be a part of
one or more allocation domain. Due to this high complexity in dealing with
hardware and software architectural interfaces, this control is not specified.
But by default, the multicore system will have an interface that affects the
allocationdomainofathread.
Consider a scenario, an operating system with three process P1, P2, P3 and
10 user level threads (T1 to T10) with a single allocation domain. 100% of CPU
resources will be distributed among all the three processes. The amount of
CPU resources allocated to each process and to each thread depends on the
contention scope, scheduling policy and priority of each thread defined by the
application developer using thread library and also depends on the system
scheduler. These User level threads are of a different contention scope.
In this case, the contention for allocation domain takes place as follows:
Process P1
All PCS threads T1, T2, T3 of Process P1 will compete among themselves.
The PCS threads of the same process can share one or more LWP. T1 and
T2 share an LWP and T3 are allocated to a separate LWP. Between T1 and
T2 allocation of kernel resources via LWP is based on preemptive priority
scheduling by the thread library. A Thread with a high priority will preempt low
priority threads. Whereas, thread T1 of process p1 cannot preempt thread T3
of process p3 even if the priority of T1 is greater than the priority of T3. If the
priority is equal, then the allocation of ULT to available LWPs is based on the
scheduling policy of threads by the system scheduler(not by thread library, in
thiscase).
Process P2
Both SCS threads T4 and T5 of process P2 will compete with processes P1
as a whole and with SCS threads T8, T9, T10 of process P3. The system
scheduler will schedule the kernel resources among P1, T4, T5, T8, T9, T10,
and PCS threads (T6, T7) of process P3 considering each as a separate
process. Here, the Thread library has no control of scheduling the ULT to the
kernel resources.
Process P3
Combination of PCS and SCS threads. Consider if the system scheduler
allocates 50% of CPU resources to process P3, then 25% of resources is for
process scoped threads and the remaining 25% for system scoped threads.
The PCS threads T6 and T7 will be allocated to access the 25% resources
based on the priority by the thread library. The SCS threads T8, T9, T10 will
divide the 25% resources among themselves and access the kernel resources
via separate LWP and KLT. The SCS scheduling is by the system scheduler.
Advantages of PCS over SCS
If all threads are PCS, then context switching, synchronization, scheduling
everything takes place within the userspace. This reduces system calls
and achieves better performance.
PCS is cheaper than SCS.
PCS threads share one or more available LWPs. For every SCS thread, a
separate LWP is [Link] every system call, a separate KLT is
created.
The number of KLT and LWPs created highly depends on the number of
SCS threads created. This increases the kernel complexity of handling
scheduling and synchronization. Thereby, results in a limitation over SCS
thread creation, stating that, the number of SCS threads to be smaller
than the number of PCS threads.
If the system has more than one allocation domain, then scheduling and
synchronization of resources becomes more tedious. Issues arise when
an SCS thread is a part of more than one allocation domain, the system
has to handle n number of interfaces.
The second boundary of thread scheduling involves CPU scheduling by the
system scheduler. The scheduler considers each kernel-level thread as a
separate process and provides access to the kernel resources.