Multi Threading.
create thread method 1-
package Multi_Threading;
class A extends Thread{
@Override
public void run(){
try{
for(int i=0; i<5; i++){
System.out.println("thread");
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println(e);
}
}
}
public class createThread1 {
public static void main(String[] args) throws InterruptedException{
A t=new A();
t.start();
for(int i=0; i<5; i++){
System.out.println("main");
Thread.sleep(1000);
}
}
}
create thread method 2-
package Multi_Threading;
class B implements Runnable{
public void run(){
for(int i=0; i<5; i++){
System.out.println("thread ji");
}
}
}
public class createThread2 {
public static void main(String[] args) {
B r = new B();
Thread t = new Thread(r);
t.start();
for(int i=0; i<5; i++){
System.out.println("main ji");
}
}
}
Interrupt Method-
package Multi_Threading;
class H extends Thread{
public void run(){
try{
for(int i=0; i<3; i++){
System.out.println("t1 thread running");
Thread.sleep(1000);// waiting state
}
}catch(InterruptedException e){
System.out.println("t1 thread terminated");
}
}
}
public class InterruptMethod {
public static void main(String[] args) {
H t1 = new H();
t1.start();
t1.interrupt();
}
}
IsAlive Method-
package Multi_Threading;
class G extends Thread{
public void run(){
System.out.println("isalive method program..");
}
}
public class IsAliveMethod{
public static void main(String[] args) {
G t1 = new G();
G t2 = new G();
t1.start();
System.out.println(t1.isAlive());
t2.start();
}
}
Join Method-
package Multi_Threading;
class D extends Thread{
public void run(){
String n = Thread.currentThread().getName();
for(int i=0; i<3; i++){
System.out.println(n);
}
}
}
public class joinMethod {
public static void main(String[] args) {
D t1 = new D();
D t2 = new D();
D t3 = new D();
t1.setName("thread 1");
t2.setName("thread 2");
t3.setName("thread 3");
t2.start();
try{
t2.join();
}catch(InterruptedException e){
System.out.println(e);
}
t1.start();
t3.start();
}
}
Stop Method-
package Multi_Threading;
class F extends Thread{
public void run(){
String n = Thread.currentThread().getName();
for(int i=0; i<3; i++){
System.out.println(n);
}
}
}
public class StopMethod {
public static void main(String[] args) {
F t1 = new F();
F t2 = new F();
F t3 = new F();
t1.setName("thread 1");
t2.setName("thread 2");
t3.setName("thread 3");
t1.start();
t2.start();
t3.start();
// t2.stop(); deprecate function
}
}
Suspend Method-
package Multi_Threading;
class suspendMethod extends Thread {
public void run(){
String n = Thread.currentThread().getName();
for(int i=0; i<3; i++){
System.out.println(n);
}
}
public static void main(String[] args) throws InterruptedException {
suspendMethod t1 = new suspendMethod();
suspendMethod t2 = new suspendMethod();
suspendMethod t3 = new suspendMethod();
t1.setName("thread 1");
t2.setName("thread 2");
t3.setName("thread 3");
t1.start();
t2.start();
//t2.suspend();
t3.start();
// t2.resume();
}
}
Thread Priority-
package Multi_Threading;
class I extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getPriority());
}
}
public class ThreadPriority {
public static void main(String[] args) {
I t1 = new I();
I t2 = new I();
I t3 = new I();
t1.setName("t1 thread");
t2.setName("t2 thread");
t3.setName("t3 thread");
t1.setPriority(2);
t2.setPriority(6);
t3.setPriority(5);
t1.start();;
t2.start();
t3.start();
}
}
Thread Scheduler-
package Multi_Threading;
class C extends Thread{
public void run(){
String n = Thread.currentThread().getName();
try{
for(int i=0; i<3; i++){
System.out.println(n);
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("InterruptedException handled");
}
}
}
public class ThreadScheduler {
public static void main(String[] args) {
C t1 = new C();
C t2 = new C();
C t3 = new C();
t1.setName("thread 1");
t2.setName("thread 2");
t3.setName("thread 3");
t1.start();;
t2.start();
t3.start();
// main thread
// String n = Thread.currentThread().getName();
// for(int i=0; i<3; i++){
// System.out.println(n);
// }
}
}
Yield-
package Multi_Threading;
class thread1 extends Thread{
public void run(){
String n=Thread.currentThread().getName();
for(int i=0; i<3; i++){
System.out.println(n);
Thread.yield();
}
}
}
class thread2 extends Thread{
public void run(){
String n=Thread.currentThread().getName();
for(int i=0; i<3; i++){
System.out.println(n);
}
}
}
public class Yield {
public static void main(String[] args) {
thread1 t1= new thread1();
thread2 t2= new thread2();
t1.start();;
t2.start();
}
}
Comments
Post a Comment