How to create a thread in java
we have two ways to creating a thread in java
- Using Thread Class
- Using Runnable Interface
When we use thread class : we have methods and constructor and we can used.click here
When we use Runnable interface : only one method : run();
Creating a thread using Extending Thread Class :
public class All extends Thread {
public void run(){
System.out.println("Thread is running using thread class");
}
public static void main(String[] args)
{
All all=new All();
all.run();
}
}
Output :
class A1 implements Runnable
{
public void run(){
System.out.println("Thread is running using runnable interface");
}
public static void main(String[] args)
{
A1 a1=new A1();
Thread t1=new Thread(a1);
t1.start();
}
}
Output :
Note : We cannot run a thread twice if we doing this exception are thrown (IllegalThreadStateException)
Example :
class TwiceThread extends Thread{
public void run(){
System.out.println("Never run twice thread");
}
public static void main(String[] args) {
TwiceThread t1=new TwiceThread();
t1.start();
t1.start();
}
}
Output :
// t1.start() first time execute
//t1.start() second time execute
Sorting and Searching Programs
Start with your Choice
-Important
Programs for Freshers
- Palindrome Number
- Prime
Number
- Swapping
Number
- Leap
Year
- Odd
and Even Number
- Fibonacci
Series
- Armstrong Number
- Factorial
Number
- Print
Table
Learn Pattern Programs
- Alphabet
pattern program in java
- Star Pattern Programs in Java
- Square
pattern programs in java
- Diamond
Star pattern programs in java
- Number
Pattern Programs in java
- Diagonal
pattern program in java
Click to learn Array Programs
- 4
ways to print Array in Java
- 2-D
Array in Java
- Anonymous
Array in Java
- Single
Dimensional Array in Java
- Find
Second Largest Number in Array
- Delete
an Element in Array in Java
- Common
Element in Array in Java
- Missing
Element in Array
- Insert
Element in Array
- Reverse Array in Java
- Merge Two Array in Java
- Smallest
and Largest Elements in Array
- Find
Odd and Even Number in Array
Click to Learn
MySQL
No comments:
Post a Comment