![]() |
linear search in Java with multiple values |
Linear search in Java
Linear search in Java: Java program to search name and print name and its corresponding other value by Linear Search
Java Tutorial, Learn Java Core Java, Python tutorial and tricks, Motivational Quotes, Interview Question Answer
![]() |
linear search in Java with multiple values |
Linear search in Java: Java program to search name and print name and its corresponding other value by Linear Search
When we want to
run multiple programs or tasks simultaneously is known as multitasking. For
example running VLC, Word, and Browser at a given time is an example of
multitasking. Every program in memory is known as a process and each process
has taken a single unit of time.
A thread is a subpart of a
process which lightweight and it is the smallest sequence of a program or a
process. Each thread has some priority and the higher the priority higher the chances
of executing preference.
The JVM controlled the life cycle of a thread and it has five
states 1. New, 2. Runnable, 3. Running, 4. Non-Runnable (Blocked) and last 5.
Terminated.
We can thread in java by two method
First, With Thread class extending
and second with implementing Runnable interface.
Java provides predefine API
to implement thread, a few of commonly used API as follows
To
create a thread we used two way first by extending Thread class and second by implementing
Runnable interface and this done by overriding the run() method. After then
start() is called.
public
class RunnableInteface implements Runnable
{
public void run()
{
System.out.println("thread is
running.. by Runnable interface");
}
public static void main(String args[])
{
RunnableInteface ob=new
RunnableInteface();
Thread t1 =new Thread(ob);
t1.start();
}
}
Output
thread
is running.. by Runnable interface
In the above program, we implement a Runnable interface. And override the run()
method and then create an object of the class and that object passed in the
Thread class constructor as Thread(ob).
public
class ExtendingThread extends Thread
{
public void run()
{
System.out.println("thread is
running..by extending class Thread.");
}
public static void main(String args[])
{
ExtendingThread t1=new
ExtendingThread();
t1.start();
}
}
Output
thread
is running..by extending class Thread.
public class DemoThread
extends Thread
{
public void run()
{
for(int i=0; i<5; i++)
{
System.out.println(getName() +
"i=" + i);
}
}
DemoThread() // constructor
{
start();
}
public static void main(String args[])
{
DemoThread ob1 = new DemoThread();
DemoThread ob2 = new DemoThread();
}
}
In the above program
run() method override and invoked by its constructor method, a loop is run from
0 to 4 and print the thread name with the help of getThread() method. In main()
method two objects are created. It will print the following output
Thread-1 i=0
Thread-1 i=1
Thread-1 i=2
Thread-1 i=3
Thread-1 i=4
Thread-0 i=0
Thread-0 i=1
Thread-0 i=2
Thread-0 i=3
Thread-0 i=4
The main difference between Daemon and User threads is, a Daemon thread is low
priority thread and User thread is a high priority thread.
Daemon
thread is used in the background supporting task and User thread is used in the foreground.
Daemon
thread life depends on the User thread whereas User Thread are independent.
public
class Daemon extends Thread {
@Override
public void run()
{
System.out.println("User Thread or
Non-Daemon Thread");
}
}
class
DaemonThread
{
public static void main(String[] args)
{
Daemon daemon = new Daemon();
daemon.start();
System.out.println("Main
Thread");
System.out.println("Is " +
daemon.getName() + " a Daemon Thread: " + daemon.isDaemon());
System.out.println("Is " +
Thread.currentThread().getName() + " a Daemon Thread: "
+ Thread.currentThread().isDaemon());
}
}
When a process communicates with synchronized threads is known as
inter-thread communication. It is used to check thread pooling in java. This can
be achieved by notify(), notifyAll() and wait() method.
Few points about the thread
import java.util.*;
public class BigNumberPrint
{
public static void main(String[] args)
{
int num1; // First Number
int num2; // Second Number
int
num3; // Third
Number
int big; // Store the largest number.
// Scanner Object sc.
Scanner sc = new Scanner(System.in);
// Input the First number from the user.
System.out.print("Enter first
number: ");
num1 = sc.nextInt();
// Input the Second number from the user.
System.out.print("Enter second
number: ");
num2 = sc.nextInt();
// Input the Third number from the user.
System.out.print("Enter third
number: ");
num3 = sc.nextInt();
// Find the largest number among three
number.
if (num1 > num2 && num1 >
num3)
{
big = num1;
}
else if (num2 > num3)
{
big = num2;
}
else
{
big = num3;
}
// Display a big number.
System.out.println("Big number:
" + big);
}
}
Output
Enter first number: 5
Enter second number: 8
Enter third number: 2
Largest number: 8
import
java.util.*;
public class
LargeNumberPrint
{
public static void main(String[] args)
{
int num1; // Input First Number
int num2; // Input Second Number
int num3; // Input Third Number
int large; // Store the largest number.
// Scanner Object sc.
Scanner sc = new Scanner(System.in);
// Input the First number from the user.
System.out.print("Enter first
number: ");
num1 = sc.nextInt();
// Input Second number from the user.
System.out.print("Enter second
number: ");
num2 = sc.nextInt();
// Input Third number from the user.
System.out.print("Enter third
number: ");
num3 = sc.nextInt();
// Find the largest number among three
numbers.
if(num1 >= num2)
{
if(num1 >= num3)
large = num1;
else
large = num3;
}
else
{
if(num2 >= num3)
large = num2;
else
large = num3;
}
// Print the large number.
System.out.println("Largest
number: " + large);
}
}
Output
Enter first number: 6
Enter second number: 9
Enter third number: 2
Largest number: 9
More Java Program
Celsius to Fahrenheit In Java – Fahrenheit To Celsius In Java
The Floyd’s triangle is a triangle of natural consecutive numbers and it is starting with a 1 to n. it is named after Robert Floyd an American computer scientist.
It looks like the following pattern
1
2 3
4 5 6
7 8 9 10
11 12 113 14 15
import java.util.*;
public class
FloydTriangle
{
public static void main(String args[])
{
int rows; // input number of rows
int i, j; // i and j used as loop
int k = 1; // k generate the floyd's
Triangle
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of
rows to generate Floyd's Triangle:");
rows = sc.nextInt();
// loop
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; j++)
{
// print the values of triangle
System.out.print(k + "
");
k++;
}
System.out.println();
}
}
}
Output:
Enter number of rows
to generate Floyd's Triangle: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Here another output
with 10 rows
Enter the number of rows
to generate Floyd's Triangle: 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36
37 38 39 40 41 42 43
44 45
46 47 48 49 50 51 52
53 54 55
We can also print with
5 rows Floyd’s triangle as
15
14 13
12 11 10
9 8 7 6
5 4 3 2 1
By just changing k=1
to k=15 and in the loop k++ to k—as below
for(i=1; i<=rows;
i++)
{
for(j=1; j<=i; j++)
{
// print the values of triangle
System.out.print(k + "
");
k--;
}
System.out.println();
}