Commit 61700739 authored by ali's avatar ali

Initial commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/AnotherSol.iml" filepath="$PROJECT_DIR$/AnotherSol.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ConcurrentList<E> {
private List<E> ls;
int count;
final ReentrantLock lock;
private final Condition notEmpty;
private final Condition notFull;
private final int max;
public ConcurrentList(List<E> ls,int max){
this(ls,max,false);
}
public ConcurrentList(List<E> ls,int max,boolean fair){
this.ls=ls;
this.max=max;
this.lock=new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
public synchronized int size() {
return ls.size();
}
public boolean isFull() throws InterruptedException {
return ls.size()==max;
}
public boolean offer(E e) throws InterruptedException {
Objects.requireNonNull(e);
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (isFull())
return false;
else {
ls.add(e);
notEmpty.signal();
return true;
}
} finally {
lock.unlock();
}
}
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (ls.size() == 0)
notEmpty.await();
E task=ls.remove(0);
notFull.signal();
return task;
} finally {
lock.unlock();
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
public class Main {
public static void main(String[] args) {
ThreadPool threadPool = new ThreadPool(3, 30);
for(int i=0; i<30; i++) {
int taskNo = i;
try {
threadPool.execute( () -> {
String message =
Thread.currentThread().getName()
+ ": Task " + taskNo ;
System.out.println(message);
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
threadPool.waitUntilAllTasksFinished();
threadPool.stop();
}
}
import java.util.List;
import java.util.concurrent.BlockingQueue;
public class PoolThreadRunnable implements Runnable{
private Thread thread = null;
private ConcurrentList<Runnable> taskList = null;
private boolean isStopped = false;
public PoolThreadRunnable(ConcurrentList<Runnable> queue){
taskList = queue;
}
public void run(){
this.thread = Thread.currentThread();
while(!isStopped()) {
try {
Runnable task =taskList.take();
task.run();
} catch (Exception e) {
}
}
}
public synchronized void doStop(){
isStopped = true;
//break pool thread out of dequeue() call.
this.thread.interrupt();
}
public synchronized boolean isStopped(){
return isStopped;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ThreadPool {
private ConcurrentList<Runnable> taskList = null;
private List<PoolThreadRunnable> runnables = new ArrayList<PoolThreadRunnable>();
private boolean isStopped = false;
public ThreadPool(int noOfThreads, int maxNoOfTasks){
taskList = new ConcurrentList<Runnable>(new ArrayList<Runnable>(maxNoOfTasks),maxNoOfTasks);
for(int i=0; i<noOfThreads; i++){
PoolThreadRunnable poolThreadRunnable =
new PoolThreadRunnable(taskList);
runnables.add(poolThreadRunnable);
}
for(PoolThreadRunnable runnable : runnables){
new Thread(runnable).start();
}
}
public synchronized void execute(Runnable task) throws Exception{
if(this.isStopped) throw
new IllegalStateException("ThreadPool is stopped");
this.taskList.offer(task);
}
public synchronized void stop(){
this.isStopped = true;
for(PoolThreadRunnable runnable : runnables){
runnable.doStop();
}
}
public synchronized void waitUntilAllTasksFinished() {
while(this.taskList.size() > 0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment