Search This Blog

Showing posts with label thread. Show all posts
Showing posts with label thread. Show all posts

Tuesday, 19 August 2014

Restrict access to Shared Data - Thread Synchronization - JAVA

How to Restrict access to shared data one thread at a time in Java - Thread Synchronization

When a data is being shared among different threads, we need to restrict the access of one thread at a time to avoid race conditions.


This program shows how to Restrict access to a shared data among different threads in Java by using thread synchronization..

ThreadCreationJava.java

public class Main {
 public static void main(String args[]){
  TargetClass target = new TargetClass();
  MyThread t1 = new MyThread(1,target); //thread id , shared object
  MyThread t2 = new MyThread(2, target);
  MyThread t3 = new MyThread(3, target);
  
  t1.start();
  t2.start();
  t3.start();
 }
}

MyThread.java

public class MyThread extends Thread {
 private int threadId;
 private TargetClass target;
 
 public MyThread(int i, TargetClass target2) {
  threadId = i; target = target2;
 }

 @Override
 public void run() {
  
  synchronized (target) { // we place shared data here. Now only one thread can access shared data at a time.
   
   try {
    sleep(2000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   target.call(threadId);
  }
  
 }
}

TargetClass.java

public class TargetClass {
 
 public void call(int threadId){
  System.out.println("Function called from thread " + threadId);
 }
 
}

Monday, 18 August 2014

How to Interrupt Thread - JAVA

How to Interrupt Thread in Java

When you run a secondary thread, it will run to completion or till an exception occurs or you explicitly interrupted from the process that created it.


This program shows how to Interrupt Thread in Java..

InterruptThreadJava.java

public class InterruptThreadJava {
 public static void main(String args[]){
  MyThread thread = new MyThread();
  thread.start();
  //stop thread after 2 iterations
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  thread.interrupt();
  System.out.println("Iterrupt called");
 }
}

MyThread.java

public class MyThread extends Thread {
 @Override
 public void run() {
  int iterator = 5;
  try {
   for (int i = 0; i < iterator; i++){
    System.out.println("From 2nd Thread:  i = " + i);
    sleep(1000);
   }
  } catch (InterruptedException e) {
   System.out.println("Thread interrupted");
  }
 }
}

Thread Creation - Implementing Runnable Interface

How to Create Threads in Java - Implement the runnable interface

Threads are used to achieve concurrency meaning running multiple tasks at the same time. One way to use threads in Java is to implement runnable interface and the other is to extend the thread class. In this chapter, We'll learn how to implement runnable interface to implemnet multi-threading in Java.

Which of these idioms should you use? The first idiom (implementing the runnable interface), which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom (extending the thread class) is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

This program shows how to use Threads in Java by implementing the runnable interface..

ThreadCreationJava.java

public class ThreadCreationJava {
 public static void main(String args[]){
  MyThread thread = new MyThread();
  thread.start();
  RunnableInterfaceClass runnableInterfaceObject = new RunnableInterfaceClass();
  new Thread(runnableInterfaceObject).run();
  
  int iterator = 3;
  try {
   for (int i = 0; i < iterator; i++){
    System.out.println("From 1st Thread:  i = " + i);
    Thread.sleep(3000);
   }
  } catch (InterruptedException e) {
   System.err.println(e);
  }
 }
}

MyThread.java

public class MyThread extends Thread {
 @Override
 public void run() {
  int iterator = 5;
  try {
   for (int i = 0; i < iterator; i++){
    System.out.println("From 2nd Thread:  i = " + i);
    sleep(2000);
   }
  } catch (InterruptedException e) {
   System.err.println(e);
  }
 }
}

RunnableInterfaceClass.java

public class RunnableInterfaceClass implements Runnable {

 @Override
 public void run() {
  int iterator = 5;
  try {
   for (int i = 0; i < iterator; i++){
    System.out.println("From 3rd Thread:  i = " + i);
    Thread.sleep(2000);
   }
  } catch (InterruptedException e) {
   System.err.println(e);
  }
 }

}

Saturday, 16 August 2014

How to Create Threads in Java - Extending the Thread Class

How to Create Threads in Java - Extending the Thread Class

Threads are used to achieve concurrency meaning running multiple tasks at the same time. One way to use threads in Java is to implement runnable interface and the other is to extend the thread class. We'll learn how to extend the thread class to implement multi-threading in Java.

Which of these idioms should you use? The first idiom (implementing the runnable interface), which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom (extending the thread class) is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.


This program shows how to use Threads in Java by extending the thread class..

ThreadCreationJava.java

public class ThreadCreationJava {
 public static void main(String args[]){
  MyThread thread = new MyThread();
  thread.start();
  
  int iterator = 3;
  try {
   for (int i = 0; i < iterator; i++){
    System.out.println("From 1st Thread:  i = " + i);
    Thread.sleep(3000);
   }
  } catch (InterruptedException e) {
   System.err.println(e);
  }
 }
}

MyThread.java

public class MyThread extends Thread {
 @Override
 public void run() {
  int iterator = 5;
  try {
   for (int i = 0; i < iterator; i++){
    System.out.println("From 2nd Thread:  i = " + i);
    sleep(2000);
   }
  } catch (InterruptedException e) {
   System.err.println(e);
  }
 }
}