java Thread creation

What is a thread and how to create a thread in java ?

Thread :
A Thread is a part of program that is a program can contain more than one thread which have separate path of
execution in memory and can work more than one task simultaneously.
A thread can create two way in java :

  • By extending Thread class
  • By implementing Runnable interface

By extending Thread class :
In this way you will simply extends Thread class and override its run() method after that just create object of main class
in main() method and call start() method. You can better understand with below example.


package com.pkjavacode.com;

public class ThreadExample extends Thread {
public void run() {
System.out.println("Run method executing...");
}

public static void main(String arg[]) {
ThreadExample t = new ThreadExample();
t.start();

}
}

 

Output :
Run method executing…

 

By implementing Runnable interface :

In this way you will simply implements Runnable interface and override its run() method after that just create object of
main class in main() method and create Thread class object and pass main class object into Thread constructor and call
start() method. You can better understand with below example.


package com.pkjavacode.com;

public class ThreadExample implements Runnable {
public void run() {
System.out.println("Run method executing...");
}

public static void main(String arg[]) {
ThreadExample te = new ThreadExample();
Thread t=new Thread(te);
t.start();

}
}

 

Output :

Run method executing…

Advertisements

What is interface in java

interface in java :

 

An interface is a contract, which define what a class can do. With interface if you will use public then you will have to write this interface in a separate file.
if you will not use public with interface then you can write interface in any class file. You can use abstract keyword with interface it’s depends on you. In interface you can also declare variable but it’s static and final type only.
When you will implements interface in a class then all method of interface should be override with public keyword within that class.

Note : An adjective should be always taken to write an interface.

Syntax :


interface <interface_name>{
method1();
method2();
.
.
.
methodn();

final int VARIABLE1;
.
.
final int VARIABLEn;

}

 
Example :

 


interface MyInterface{
void display();
}

public class MyDemo implements MyInterface {
public void display(){
System.out.println("display method executing...");
}
public static void main(String[] args) {
MyInterface e=new MyDemo();
e.display();
}
}

&nbsp;

Implements comparator in java


package com.pk.com;

import java.util.*;

public class ComparatorDemo 
{
public static void main(String args[])
{
String str[]={"pky", "aky", "sky"};
TestComp tc=new TestComp();
Arrays.sort(str, tc);
for(String str1 : str)
System.out.println(str1);
}
static class TestComp implements Comparator<String>
{
public int compare(String a, String b)
{
return b.compareTo(a);
}
}
}