Programming Questions


Programming Questions

1. Find next greater number using the same digits for a given number?

Java Coding

1. Write an implementation for Simple ThreadPool?

Pictorical depiction
Implementation

Queue.java

1
2
3
4
5
6
package com.javaforlazyreaders.utils;

public interface Queue<T> {
	public void enqueue(T element);
	public T dequeue();
}

QueueImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.javaforlazyreaders.utils;

import java.util.ArrayList;
import java.util.List;

public class QueueImpl<T> implements Queue<T>{
	
	private List<T> list = new ArrayList();

	@Override
	public void enqueue(T element) {
		synchronized(list) {
			list.add(element);
			list.notifyAll();
		}		
	}

	@Override
	public T dequeue() {
		T element = null;
		synchronized(list) {
			while(list.isEmpty()) {
				try {
					list.wait();
				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
					break;
				}
			}
			
			if(!Thread.currentThread().isInterrupted())
				element = list.remove(0);
		}		
		return element;	
	}
}

Task.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.javaforlazyreaders.utils;

public class Task {
	
	private Runnable runnable;
	public Task(Runnable runnable) {
		this.runnable = runnable;
	}
	
	public void run() {
		runnable.run();
	}
}

WorkerThread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.javaforlazyreaders.utils;

import java.util.List;

public class WorkerThread extends Thread{
	private String name;
	private boolean isStopped;
	private Queue<Task> taskList;
	public WorkerThread(String name, Queue<Task> taskList) {
		isStopped = false;
		this.taskList = taskList;
		this.name = name;
	}
	
	@Override
	public void run() {
		while(!isStopped) {
			Task t = taskList.dequeue();
			if(t != null) {
				System.out.println(name + " ");
				t.run();
			}
		}
		System.out.println("Exiting " + name);
	}
	
	public void shutdown() {
		isStopped = true;
		
		this.interrupt();
	}
}

CustomThreadPool.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.javaforlazyreaders.utils;

import java.util.ArrayList;
import java.util.List;

public class CustomThreadPool {

	private WorkerThread [] threads;
	private Queue<Task> taskList = new QueueImpl<Task>();	
	private int poolSize;
	private boolean isShutdown;
	
	public CustomThreadPool(int poolSize) {
		this.poolSize = poolSize;
		isShutdown = false;
		threads = new WorkerThread[poolSize];
		for(int i = 0; i < poolSize; i++) {
			WorkerThread t = new WorkerThread("WorkerThread:" + i, taskList);
			threads[i] = t;
			threads[i].start();
			
		}
	}
	
	public void submit(Runnable runnable) {
		taskList.enqueue(new Task(runnable));
	}
	
	public void shutdown() {
		isShutdown = true;
		for(int i = 0; i < poolSize; i++) {
			threads[i].shutdown();
		}
	}	
}

ThreadPoolDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.javaforlazyreaders.demo;

import com.javaforlazyreaders.utils.CustomThreadPool;
import com.javaforlazyreaders.utils.QueueImpl;

public class ThreadPoolDemo {

	public static void main(String[] args) {
		
		
		CustomThreadPool threadPool = new CustomThreadPool(3);
		
		for(int i = 0; i < 20; i++) {
			threadPool.submit(new MyRunnable("T" + i));
		}
		
		try {
			Thread.sleep(30000);
		} catch(InterruptedException ie) {
			System.out.println("Main thread interrupted");
		}
		
		threadPool.shutdown();
		System.out.println("Done");
	}

}

class MyRunnable implements Runnable {

	private String name;
	
	public MyRunnable(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void run() {
		try {
			System.out.println("Executing : " + name);
			Thread.sleep(1000);
		}catch(InterruptedException ie) {
			System.out.println("Interrupted : " + name);
		}
		System.out.println("Exiting " + name);
	}	
}