Commit 92c477df authored by mohammad.salama's avatar mohammad.salama

Working but facing unhandled exception

parents
<?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="PersonalThreadPool" />
</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_1_8" default="true" project-jdk-name="1.8" 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="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="00b47615-8913-4160-9c73-190821c9c0e9" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/MyThreadPool.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/MyThreadPoolRunnable.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/TasksQueue.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2YfRg5HWBulipBOuD9eFp0wMKb0" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="00b47615-8913-4160-9c73-190821c9c0e9" name="Changes" comment="" />
<created>1700914756140</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1700914756140</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</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>PersonalThreadPool</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
public class Main
{
public static void main(String[] args) throws Exception
{
MyThreadPool myThreadPool = new MyThreadPool<>(3 , 50);
for(int i = 0; i < 100; i++)
{
int taskNo = i;
myThreadPool.execute(new Runnable() {
@Override
public void run() {
String message = Thread.currentThread().getName() + ": Task " + (taskNo + 1) ;
System.out.println(message);
}
});
}
/* myThreadPool.waitUntilAllTasksFinished(1000);
myThreadPool.stop();*/
}
}
import java.util.ArrayList;
import java.util.List;
public class MyThreadPool <RunnableType extends Runnable>
{
private TasksQueue<RunnableType> tasksQueue;
private List<MyThreadPoolRunnable> workingThreads;
private boolean isTerminated;
private boolean allowInserting;
private int queueSize;
private int workingThreadsNumber;
public MyThreadPool(int queueSizeInput, int workingThreadsNumberInput) throws Exception
{
if (queueSizeInput <=0 || workingThreadsNumberInput <= 0)
{
throw new Exception("Queue Size and Threads Number must be Strictly Larger than zero!!");
}
this.queueSize = queueSizeInput;
this.workingThreadsNumber = workingThreadsNumberInput;
this.isTerminated = false;
this.allowInserting = true;
initiateThreadPool();
startWorking();
}
public void execute (RunnableType runnableType) throws Exception
{
if (isTerminated || !allowInserting) return;
this.tasksQueue.addTask(runnableType);
}
public void stop()
{
synchronized (this)
{
this.isTerminated = true;
this.allowInserting = false;
for (MyThreadPoolRunnable runnable : workingThreads)
runnable.forceStop();
}
}
public synchronized void waitUntilAllTasksFinished(long timeOut) throws InterruptedException
{
if (this.isTerminated) return;
allowInserting = false;
this.isTerminated = true;
tasksQueue.softFinish(timeOut);
}
private void initiateThreadPool()
{
this.tasksQueue = new TasksQueue<>(this.queueSize);
this.workingThreads = new ArrayList<>();
for (int i=1 ; i<= this.workingThreadsNumber ; i++)
{
workingThreads.add(new MyThreadPoolRunnable(tasksQueue));
}
}
private void startWorking()
{
for (MyThreadPoolRunnable myThreadPoolRunnable : workingThreads)
new Thread(myThreadPoolRunnable).start();
}
}
public class MyThreadPoolRunnable<RunnableType extends Runnable> implements Runnable
{
private TasksQueue<RunnableType> tasksQueue;
private boolean isTerminated;
private Thread thread;
public MyThreadPoolRunnable(TasksQueue<RunnableType> tasksQueue)
{
this.tasksQueue = tasksQueue;
this.isTerminated = false;
}
public void forceStop()
{
this.isTerminated = true;
this.thread.interrupt();
}
@Override
public void run()
{
thread = Thread.currentThread();
while(!terminated())
{
try
{
RunnableType job = tasksQueue.removeTask();
if (job == null) continue;
job.run();
}
catch (InterruptedException e)
{
//ignore
}
}
}
private synchronized boolean terminated()
{
return this.isTerminated;
}
}
import java.util.ArrayList;
import java.util.List;
public class TasksQueue <RunnableType>
{
private List<RunnableType> tasks;
private int maxSize;
private boolean allowInsertion;
public TasksQueue(int maxCap)
{
this.maxSize = maxCap;
tasks = new ArrayList<>();
allowInsertion = true;
}
public void addTask(RunnableType task) throws Exception
{
if (task == null)
{
throw new Exception("Task Assigned is NULL");
}
synchronized (this)
{
if (!allowInsertion) return;
if (tasks.size() == maxSize)
{
///System.out.println("Stuck in Adding");
this.wait();
}
tasks.add(task);
this.notifyAll();
}
}
public RunnableType removeTask () throws InterruptedException
{
RunnableType task = null;
synchronized (this)
{
if (tasks.size()==0 && allowInsertion)
{
///System.out.println("Stuck in Removing");
this.wait();
}
if (tasks.size() ==0 && !allowInsertion) return task;
if(tasks.size() == 0)
{
/*System.out.println("HHHHHHHHHHHH");
System.out.println("!allow" + !allowInsertion);*/
}
task = tasks.remove(0);
this.notifyAll();
}
return task;
}
public void softFinish(long timeOut) throws InterruptedException
{
allowInsertion = false;
while (this.size() > 0)
{
this.wait(timeOut);
}
}
private int size()
{
synchronized (tasks)
{
return tasks.size();
}
}
}
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