0% found this document useful (0 votes)
8 views14 pages

Unit III Java

The document covers key concepts in Java including interfaces, packages, and multithreading. It explains how to define and implement interfaces, create and use packages, and the basics of multithreading, including thread lifecycle and priorities. Examples are provided to illustrate the implementation of these concepts in Java programming.

Uploaded by

rajucharan20009
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)
8 views14 pages

Unit III Java

The document covers key concepts in Java including interfaces, packages, and multithreading. It explains how to define and implement interfaces, create and use packages, and the basics of multithreading, including thread lifecycle and priorities. Examples are provided to illustrate the implementation of these concepts in Java programming.

Uploaded by

rajucharan20009
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
You are on page 1/ 14

Unit-III– Interface, Packages,

Mutithreading…..
Interface

• Interface looks like a class but it is not a class.


• An interface can have methods and variables just like the class but the methods
declared in interface are by default abstract (only declaration, no body)
• Also, the variables declared in an interface are constants

Syntax for defining an interface

interface MyInterface
{
datatype variable=constant;
public void method1();
public void method2();

To implement an interface

class classname implements interfacename


{
body of class
}
Program to demonstrate the concept of interface:
interface circle
{
double pi=3.14;
double radius=5.5;
void compute();
}

class area implements circle


{
public void compute()
{
double result;
result=pi*radius*radius;
System.out.println(“Area of Circle=”+result);
}
}

class circumference implements circle


{
public void compute()
{
double result;
result=2*pi*radius ;
System.out.println(“Circumference of Circle=”+result);
}
}
class interfacedemo
{
public static void main(String args[])
{
area a=new area();
a.compute();

circumference c=new circumference();


c.compute();
}
}

Output:

Area of circle= 94.98


Circumference of Circle=34.54
Packages

• PACKAGE in Java is a collection of classes, sub-packages, and interfaces.


• It helps to organize your classes into a folder structure and make it easy to
locate and use them.
•More importantly, it helps to improve code reusability.
• Package in java can be categorized in two form, built-in packages and
user-defined packages.
• There are many built-in packages such as java, lang, awt, javax, swing, net, io,
util, sql etc.
Built-in Packages in Java

To use a class or a package from the library, you need to use


the import keyword:

Example to import a package in program

import java.util.*;
import java.applet.Applet;
How to create our own package in Java
•First create a directory within name of package.
•Create a java file in newly created directory.
•In this java file you must specify the package name with the help
of package keyword.
•Save this file with same name of public class
package pack1;

public class Demo


{
public void show()
{
System.out.println(“Package called”);
}
}

After that use this package in your program


import pack1.*;
class A
{
public static void main(String args[])
{
Demo d=new Demo();
d.show();
}
}
Multithreading

• Multithreading is a concept where a program is divided into two or more subprograms,


which can be executed at the same time in parallel. This is also known as
multithreading.
• A thread is similar to a program that has a single flow of control
•The process of executing multiple threads simultaneously is known as
multithreading.

Advantages of Mutithreading
1.The main purpose of multithreading is to provide simultaneous execution of two or
more parts of a program to maximum utilize the CPU time
2.It enables programmers to do many things at one time
3.They can a long program into many threads and execute them in parallel.
Steps to Create a thread in java
• create a new class that extends Thread, then override the run() method and then to
create an instance of that class.
•The run() method is what is executed by the thread after you call start().

• Here is an example of creating a Java Thread subclass:

public class MyClass extends Thread


{
public void run()
{
System.out.println("MyClass running");
}
}

To create and start the above thread:

MyClass t1 = new MyClass ();


T1.start();
When the run() method executes it will print out the text " MyClass running ".
Example program to create three threads and display the strings using multithreading
concept

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println(“Welcome to C”);
}

class B extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println(“Welcome to C++”);
}

class C extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println(“Welcome to Java”);
}

}
class threaddemo
{
public static void main(String args[])
{
A a=new A();
a.start();

B b=new B();
b.start();

C c=new C();
c.start();

}
}
Life cycle of a thread

A thread goes through various stages in its lifecycle. The following diagram
shows the complete life cycle of a thread.
Following are the stages of the life cycle −

New − A new thread begins its life cycle in the new state. It remains in this state until
the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread waits
for another thread to perform a task. Thread transitions back to the runnable state only
when another thread signals the waiting thread to continue executing.
Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.

1
3
Thread Priority

•Each thread have a priority.


•Priorities are represented by a number between 1 and 10.

3 constants defined in Thread class:

public static int MIN_PRIORITY


public static int NORM_PRIORITY
public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY


is 1 and the value of MAX_PRIORITY is 10.

1
4

You might also like