Tags » Threads

Threads - Deadlock

package com.mycompany.threadcreation;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author chaitanya
*/
public class ThreadDeadlock {
/**
* @param args the command line arguments… 537 more words

Threads

Threads - ProducerConsumer design using wait,notify and synchronization

package com.mycompany.threadcreation;

import java.util.LinkedList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Producer Consumer design by using Wait,Notify and Synchronization
* @author chaitanya
*/
public class ProducerConsumer { 231 more words

Threads

Threads - ExecutorService example

package com.mycompany.threadcreation;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author chaitanya
*/
public class ThreadExecutorService {

/**
* @param args the command line arguments… 136 more words

Threads

Threads: Off the Cuff

Written and Produced by: Monica Solis

Click here to download this episode of Threads.

With a new year comes new resolutions. But they’re not that new. 782 more words

KTSW

Printing odd and even characters with 2 threads

</pre>
public class PrintName {
 public final static String USER_INPUT = "jagadessh";

 public static void main(String[] args) {
  PrintThread p1 = new PrintThread(USER_INPUT);

  Thread t2 = new Thread(p1, "EVEN");
  Thread t1 = new Thread(p1, "ODD");

  t1.start();
  t2.start();
 }
}

public class PrintThread implements Runnable {

 private final Object object = new Object();

 private int index;

 PrintThread(String name) {
  this.name = name;
  this.index = 0;
 }

 public String name;

 @Override
 public void run() {
  try {
   if (Thread.currentThread().getName().equals("ODD")) {
    printOdd();
   } else {
    printEven();
   }
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

 private void printEven() throws InterruptedException {
  while (index < name.length()) {
   synchronized (object) {
    if (index % 2 == 0) {
     object.wait();
    }
    if (index < name.length()) {
     System.out.println(Thread.currentThread().getName() + " " + name.charAt(index));
     object.notify();
     index++;
    }
   }
  }
 }

 private void printOdd() throws InterruptedException {
  while (index < name.length()) {
   synchronized (object) {
    if (index % 2 != 0) {
     object.wait();
    }
    if (index < name.length()) {
     System.out.println(Thread.currentThread().getName() + " " + name.charAt(index));
     object.notify();
     index++;
    }
   }
  }
 }
}
Java Core

America the Right Way -Staying Vigilant- April 21, 2014 – April 25, 2014 Remember 9/11

Hello!

Tomorrow Boston will host the first Marathon since last year’s when terrorists daringly and horrifically attacked runners and spectators as they neared the finish line. 61 more words

Threads

New Items in Shop

I have finally got round to stocking up my shop. All the items listed have been coloured using natural dyes. I have sat on them for a long time (at least a year) because I wanted to see what would happen with the passing of time and I am happy to report that they have remained the same as the day they were dyed. 54 more words

Natural Dyeing