0% found this document useful (0 votes)
5 views15 pages

Lecture - 10 (Java) E-Notes

The document covers Java packages and multithreading, explaining predefined and user-defined packages, along with their structure and usage. It details the concept of threads, their lifecycle, and the difference between multithreading and multitasking. Additionally, it provides examples of creating threads by extending the Thread class and implementing the Runnable interface.

Uploaded by

samanraj823004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Lecture - 10 (Java) E-Notes

The document covers Java packages and multithreading, explaining predefined and user-defined packages, along with their structure and usage. It details the concept of threads, their lifecycle, and the difference between multithreading and multitasking. Additionally, it provides examples of creating threads by extending the Thread class and implementing the Runnable interface.

Uploaded by

samanraj823004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Training On Java

Lecture – 10
Package And Multithreading
Package In Java

java-language:- In java James Gosling is maintained predefined support in the form of packages and these
packages contains classes & interfaces, and these classes and interfaces contains predefined methods &
variables.
Java-language--- packages----classes and interfaces------methods and variables
Types of packages:- There are two types of packages in java
1) Predefined packages.
2) User defined packages
Package In Java (cont..)

Predefined packages: The predefined packages are introduced by James Gosling and these packages
contains predefined classes & interfaces and these class & interfaces contains predefined variables and
methods. Example:- [Link], [Link] , [Link]…..etc.
User defined packages:-
 The packages which are defined by user, and these packages contains user defined classes and
interfaces.
 Declare the package by using package keyword.
Syntax : package package-name;
Example : package [Link];
 Inside the source file it is possible to declare only one package statement and that statement must be
first statement of the source file
Example Application - 1
Step-1: write the application with package statement.
package [Link];
class Test
{
public static void main(String[] args)
{
[Link]("package first example");
}
}
class A {
}
class B {
}
interface It {
}
Example Application – 1 (cont..)
Step-2:- compilation process If the source file contains the package statement then compile that application by
using fallowing command.
Example Application – 1 (cont..)
Step-3:- folder Structure.
com
|--------softpro
|------java
|-----corejava
|----[Link]
|----[Link]
|-----[Link]
|----[Link]
Step-4:-execution process.

Execute the .class file by using fully qualified name(class name with complete package structure).
java [Link]
output : package first example
Multithreading In Java

1) Thread is nothing but separate path of sequential execution.


2) The independent execution technical name is called thread.
3) Whenever different parts of the program executed simultaneously that each and every part is called thread.
4) The thread is light weight process because whenever we are creating thread it is not occupying the separate
memory it uses the same memory. Whenever the memory is shared means it is not consuming more memory.
5) Executing more than one thread a time is called multithreading.
Difference Between Multithreading & Multitasking

Multitasking Multithreading
In multitasking, users are allowed to perform many While in multithreading, many threads are created from
tasks by CPU. a process through which computer power is increased.
Multitasking involves often CPU switching between While in multithreading also, CPU switching is often
the tasks. involved between the threads.
In multitasking, the processes share separate memory. While in multithreading, processes are allocated same
memory.
Multitasking component involves multiprocessing. While multithreading component does not involve
multiprocessing.
In multitasking, CPU is provided in order to execute While in multithreading also, CPU is provided in order
many tasks at a time. to execute many threads from a process at a time.
In multitasking, processes don’t share same resources, While in multithreading, each process share same
each process is allocated separate resources. resources.
Thread Life Cycle

A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in
java new, runnable, non-runnable and terminated. There is no running state. But for better understanding the
threads, we are explaining it in the 5 states. The life cycle of the thread in java is controlled by JVM. The java
thread states are as follows:
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
Thread Life Cycle (cont..)
Thread Life Cycle (cont..)

1) New:-The thread is in new state if you create an instance of Thread class but before the invocation of
start() method.
2) Runnable:-The thread is in runnable state after invocation of start() method, but the thread scheduler has
not selected it to be the running thread.
3) Running:-The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked):-This is the state when the thread is still alive, but is currently not eligible to
run.
5) Terminated:-A thread is in terminated or dead state when its run() method exits.
Creating Thread
A thread can be created in two ways:-
1) By extending Thread class.
2) By implementing [Link] interface.

First approach to create thread extending Thread class:-

Step 1:- Our normal java class will become Thread class whenever we are extending predefined Thread class.
class MyThread extends Thread {
};
Step 2:- override the run() method to write the business logic of the Thread (run() method present in Thread
class).
class MyThread extends Thread {
public void run() {
[Link]("business logic of the thread");
[Link]("body of the thread");
}
}
Creating Thread (cont..)

Step 3:- Create userdefined Thread class object.

MyThread t=new MyThread();

Step 4:- Start the Thread by using start() method of Thread class.

[Link]();
Example Application - 2
//Creating thread by extending Thread class
class MyThread extends Thread {
//business logic of user defined Thread
public void run() {
for (int i=0;i<10;i++) {
[Link]("userdefined Thread");
} } };
class ThreadDemo {
public static void main(String[] args) {
MyThread t=new MyThread(); //MyThread is created
[Link](); //MyThread execution started
//business logic of main Thread
for (int i=0;i<10;i++) {
[Link]("Main Thread");
} } };
Example Application - 3
//Creating thread by implementing Runnable interface
class MyThread extends Thread {
class MyThread implements Runnable {
public void run() { //business logic of user defined Thread
for (int i=0;i<10;i++) {
[Link]("userdefined Thread");
} } };
class ThreadDemo {
public static void main(String[] args){ //main thread started
MyThread r=new MyThread(); //MyThread is created
Thread t=new Thread(r);
[Link](); //MyThread execution started
//business logic of main Thread
for (int i=0;i<10;i++)
{
[Link]("Main Thread");
} }}

You might also like