Java Threading Concept ..

8 Sep

Thread Pool Worker :

=====================

/*In above program, we are creating fixed size thread pool of 5 worker threads.
* Then we are submitting 10 jobs to this pool,
* since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state,
*  as soon as one of the job is finished, another job from
*  the wait queue will be picked up by worker thread and get’s executed.
* */

package com.soumya.test.prep;

/*In above program, we are creating fixed size thread pool of 5 worker threads.
* Then we are submitting 10 jobs to this pool,
* since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state,
*  as soon as one of the job is finished, another job from
*  the wait queue will be picked up by worker thread and get’s executed.
* */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread(“” + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println(“Finished all threads”);
}

}

package com.soumya.test.prep;

public class WorkerThread implements Runnable {

private String command;

public WorkerThread(String s){
this.command=s;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+”Start. Command = “+command);
processCommand();
System.out.println(Thread.currentThread().getName()+” End.”);
}

private void processCommand() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public String toString(){
return this.command;
}
}

Simple DealLockExample :

==========================

package com.soumya.test.prep;

public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format(“%s: %s”
+ ”  has bowed to me!%n”,
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format(“%s: %s”
+ ” has bowed back to me!%n”,
this.name, bower.getName());
}
}

public static void main(String[] args) {
final Friend alphonse =
new Friend(“Alphonse”);
final Friend gaston =
new Friend(“Gaston”);
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}

Link List Implementation using JAVA

3 Mar

package com.test.corejava;

 

class Link {

  public int iData;

  public Link next;

 

  public Link(int id) {

    iData = id;

  }

 

  public String toString() {

    return “{” + iData+ “} “;

  }

}

 

class LinkList {

  private Link first;

 

  public LinkList() {

    first = null;

  }

 

  public boolean isEmpty() {

    return (first == null);

  }

 

  public void insertFirst(int id) {

    Link newLink = new Link(id);

    newLink.next = first;

    first = newLink;

  }

 

  public Link deleteFirst() {

    Link temp = first;

    first = first.next;

    return temp;

  }

 

  public Link deleteSpecificIndex(int index)

  {

  Link current = first;

  Link previous=first;

  while(current.iData !=index)

  {

        if(current.next ==null)

        return null;

        else

        {

        previous=current;

         

        current=current.next;

         

        }

 

  }

 

 

  if(current==first)

  first=first.next;

  else

  previous.next=current.next;

  return current;

  }

 

  public Link insertIntoSpecificIndex(int index)

  {

  Link current =first;

  Link previous = first;

  Link newlink = new Link(index);

 

  for(int i=0;i<index;i++)

  {

 

  previous=current;

  current=current.next;

  }

  previous.next=newlink;

  newlink.next=current;

 

  return current;

 

  }

 

  public Link insertLast()

  {

  Link current =first;

  Link newlink = new Link(20);

  while(current.next!=null)

  {

  //previous = current; 

  current = current.next;

  }

 

  current.next = newlink;

  current=current.next;

  return current;

  }

 

  public String toString() {

    String str = “”;

    Link current = first;

    while (current != null) {

      str += current.toString();

      current = current.next;

    }

    return str;

  }

}

 

public class MainClass {

  public static void main(String[] args) {

    LinkList theList = new LinkList();

 

    theList.insertFirst(22);

    theList.insertFirst(44);

    theList.insertFirst(66);

    theList.insertFirst(88);

    //theList.insertIntoSpecificIndex(4);

    theList.insertLast();

//Link current=    theList.deleteSpecificIndex(88);

//System.out.println(current.iData);

 

    System.out.println(theList);

 

    while (!theList.isEmpty()) {

      Link aLink = theList.deleteFirst();

      System.out.print(“Deleted ” + aLink);

      System.out.println(“”);

    }

    System.out.println(theList);

  }

}

Create your own website, free on WordPress.com.Create your own website.
Start free