Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
T
ThreadSyncing-PP03
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohammad.salama
ThreadSyncing-PP03
Commits
92c477df
Commit
92c477df
authored
Nov 25, 2023
by
mohammad.salama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Working but facing unhandled exception
parents
Changes
16
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
372 additions
and
0 deletions
+372
-0
compiler.xml
PoolThreadProject/PersonalThreadPool/.idea/compiler.xml
+14
-0
encodings.xml
PoolThreadProject/PersonalThreadPool/.idea/encodings.xml
+8
-0
jarRepositories.xml
...hreadProject/PersonalThreadPool/.idea/jarRepositories.xml
+21
-0
misc.xml
PoolThreadProject/PersonalThreadPool/.idea/misc.xml
+15
-0
vcs.xml
PoolThreadProject/PersonalThreadPool/.idea/vcs.xml
+7
-0
workspace.xml
PoolThreadProject/PersonalThreadPool/.idea/workspace.xml
+74
-0
pom.xml
PoolThreadProject/PersonalThreadPool/pom.xml
+18
-0
Main.java
PoolThreadProject/PersonalThreadPool/src/main/java/Main.java
+21
-0
MyThreadPool.java
...roject/PersonalThreadPool/src/main/java/MyThreadPool.java
+74
-0
MyThreadPoolRunnable.java
...ersonalThreadPool/src/main/java/MyThreadPoolRunnable.java
+45
-0
TasksQueue.java
...dProject/PersonalThreadPool/src/main/java/TasksQueue.java
+75
-0
Main$1.class
...eadProject/PersonalThreadPool/target/classes/Main$1.class
+0
-0
Main.class
...hreadProject/PersonalThreadPool/target/classes/Main.class
+0
-0
MyThreadPool.class
...ject/PersonalThreadPool/target/classes/MyThreadPool.class
+0
-0
MyThreadPoolRunnable.class
...sonalThreadPool/target/classes/MyThreadPoolRunnable.class
+0
-0
TasksQueue.class
...roject/PersonalThreadPool/target/classes/TasksQueue.class
+0
-0
No files found.
PoolThreadProject/PersonalThreadPool/.idea/compiler.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/.idea/encodings.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/.idea/jarRepositories.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/.idea/misc.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/.idea/vcs.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/.idea/workspace.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/pom.xml
0 → 100644
View file @
92c477df
<?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
PoolThreadProject/PersonalThreadPool/src/main/java/Main.java
0 → 100644
View file @
92c477df
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();*/
}
}
PoolThreadProject/PersonalThreadPool/src/main/java/MyThreadPool.java
0 → 100644
View file @
92c477df
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
();
}
}
PoolThreadProject/PersonalThreadPool/src/main/java/MyThreadPoolRunnable.java
0 → 100644
View file @
92c477df
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
;
}
}
PoolThreadProject/PersonalThreadPool/src/main/java/TasksQueue.java
0 → 100644
View file @
92c477df
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
();
}
}
}
PoolThreadProject/PersonalThreadPool/target/classes/Main$1.class
0 → 100644
View file @
92c477df
File added
PoolThreadProject/PersonalThreadPool/target/classes/Main.class
0 → 100644
View file @
92c477df
File added
PoolThreadProject/PersonalThreadPool/target/classes/MyThreadPool.class
0 → 100644
View file @
92c477df
File added
PoolThreadProject/PersonalThreadPool/target/classes/MyThreadPoolRunnable.class
0 → 100644
View file @
92c477df
File added
PoolThreadProject/PersonalThreadPool/target/classes/TasksQueue.class
0 → 100644
View file @
92c477df
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment