3/20/19

Multi thread Programming in Java | Part -1

  • Java is the first widely used programming language to provide support for concurrent programming at the language level
  • Java supports concurrency through the use of threads, and provides mechanisms for synchronization between threads
In Java, thread instances come in two varieties:

java.lang.Thread
Extend the java.lang.Thread class (and override its run() method)

java.lang.Runnable 
interface and associate an instance of your class with java.lang.Thread. Implement run() method

Note:
Most important method: run()
The default version of this method does nothing so it must be overridden.
Should call start() to begin execution of the new thread.
If you are not overriding any of Thread’s other methods, can simply implement runnable.


Example: extending Thread

package com.itofnajathi.www;

class Hi extends Thread {
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hi");
try { Thread.sleep(500); } catch(Exception ex){}
}
}
}

class Hello extends Thread {
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hello");
try{ Thread.sleep(500); } catch(Exception ex){}
}
}
}

public class ThreadDemo {

public static void main(String[] args) {

Hi obj1 = new Hi();
Hello obj2 = new Hello();
//Diffrence between start() and run()
//If you call start() method, you can run parallaly (multi threading)
obj1.start();
try{ Thread.sleep(10); } catch(Exception ex){}
obj2.start();
//Diffrence between start() and run()
//If you call run() method, you can't run parallaly (This is not multi threading)
//obj1.run();
//obj2.run();
}

}




Example: Runnable interface

package com.itofnajathi.www;

class Hii implements Runnable {
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hi");
try{Thread.sleep(500);} catch(Exception Ex){}
}
}
}

class Helloo implements Runnable {
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hello");
try{Thread.sleep(500);} catch(Exception ex){}
}
}
}

public class RunnableDemo {

public static void main(String[] args) {

Runnable obj1 = new Hii();
Runnable obj2 = new Helloo();
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
t1.start();
try{Thread.sleep(10);} catch(Exception ex){}
t2.start();
}

}



Example: Thread Synchronization

package com.itofnajathi.www;

class Counter{
int count;
public synchronized void increment(){
count++;
}
}

public class SynchronizedDemo {

public static void main(String[] args) throws Exception {

Counter c = new Counter();
//c.increment();c.increment();
Thread t1 = new Thread(new Runnable() {
public void run(){
for(int i=1;i<=1000;i++){
c.increment();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run(){
for(int i=1;i<=1000;i++){
c.increment();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Count "+c.count);
}

}

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *