Commit f6a56a2c authored by ubay.alshamali's avatar ubay.alshamali

Initial commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="ThreadPoolImplementaion" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" 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 xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ThreadPoolImplementaion</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
public class Main
{
public static void main(String[] args) throws Exception {
ThreadPool threadPool = new ThreadPool(3, 10);
for(int i=0; i<10; i++) {
int taskNo = i;
threadPool.execute( () -> {
String message =
Thread.currentThread().getName()
+ ": Task " + taskNo ;
System.out.println(message);
});
}
threadPool.Wait_Xms_UntillAllTasksFinished(1000);
threadPool.Stop();
}
}
import java.util.LinkedList;
import java.util.Queue;
public class MyBlockingQueue <T>
{
private Queue<T> queue;
private int Q_Size;
public MyBlockingQueue(int q_Size)
{
queue = new LinkedList<>();
this.Q_Size = q_Size;
}
public synchronized void Enqueue(T element) throws InterruptedException
{
while(queue.size() == Q_Size)
{
wait();
}
queue.offer(element);
notify();
}
public synchronized T dequeue() throws InterruptedException
{
while(queue.isEmpty())
{
wait();
}
T element = queue.poll();
notify();
return element;
}
public synchronized int Size()
{
return queue.size();
}
}
public class PoolThreadRunnable implements Runnable
{
Thread thread = null;
MyBlockingQueue<Runnable> taskqueue = null;
boolean isStopped = false;
public PoolThreadRunnable(MyBlockingQueue queue)
{
taskqueue = queue;
}
@Override
public void run()
{
this.thread = Thread.currentThread();
while (!isStopped)
{
try
{
Runnable task = taskqueue.dequeue();
task.run();
}
catch (InterruptedException e)
{
//do nothing,,, cause task might be null
}
}
}
public synchronized void Stop()
{
isStopped = true;
this.thread.interrupt();
}
public synchronized boolean isStopped()
{
return isStopped;
}
}
import java.util.ArrayList;
import java.util.List;
public class ThreadPool
{
MyBlockingQueue<Runnable> taskqueue = null;
List<PoolThreadRunnable> threads = new ArrayList<PoolThreadRunnable>();
boolean isStopped = false;
public ThreadPool(int n_threads, int q_size)
{
this.taskqueue = new MyBlockingQueue<>(q_size);
for(int i = 0; i<n_threads; i++)
{
PoolThreadRunnable thread = new PoolThreadRunnable(taskqueue);
threads.add(thread);
}
for (PoolThreadRunnable thread: threads)
{
new Thread(thread).start();
}
}
public synchronized void execute(Runnable task) throws Exception
{
if(isStopped)
throw new IllegalStateException("thread is stopped");
taskqueue.Enqueue(task);
}
public synchronized void Stop()
{
isStopped = true;
for(PoolThreadRunnable thread: threads)
{
thread.Stop();
}
}
public synchronized void Wait_Xms_UntillAllTasksFinished(int duration_ms)
{
if(taskqueue.Size() > 0)
{
try
{
Thread.sleep(duration_ms);
}
catch (InterruptedException e)
{
System.out.println(e.getStackTrace());
}
}
}
}
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