Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
E
Executor_Service
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
yazan.halloul
Executor_Service
Commits
ad2914d6
Commit
ad2914d6
authored
Nov 15, 2023
by
yazan.halloul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add worker, Prime Counters and Parallel Counters commit
parent
d800566d
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
462 additions
and
0 deletions
+462
-0
ParallelCounter0.java
src/main/java/parallelCounters/ParallelCounter0.java
+50
-0
ParallelCounter1.java
src/main/java/parallelCounters/ParallelCounter1.java
+47
-0
ParallelCounter2.java
src/main/java/parallelCounters/ParallelCounter2.java
+47
-0
ParallelCounter3.java
src/main/java/parallelCounters/ParallelCounter3.java
+57
-0
ParallelCounter4.java
src/main/java/parallelCounters/ParallelCounter4.java
+52
-0
PrimeCounter.java
src/main/java/primeCounters/PrimeCounter.java
+20
-0
PrimeCounterCallable.java
src/main/java/primeCounters/PrimeCounterCallable.java
+31
-0
PrimeCounterRunnable.java
src/main/java/primeCounters/PrimeCounterRunnable.java
+38
-0
PrimeCounterThread.java
src/main/java/primeCounters/PrimeCounterThread.java
+43
-0
WorkPartitioner.java
src/main/java/worker/WorkPartitioner.java
+38
-0
Worker.java
src/main/java/worker/Worker.java
+39
-0
No files found.
src/main/java/parallelCounters/ParallelCounter0.java
0 → 100644
View file @
ad2914d6
package
parallelCounters
;
import
java.util.ArrayList
;
import
java.util.List
;
import
primeCounters.PrimeCounterThread
;
import
worker.WorkPartitioner
;
/**
* This class calculates the sum of an array using {@link PrimeCounterThread} and conventional thread
* start and join mechanisms.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
ParallelCounter0
{
public
static
long
findPrimeNumbers
(
int
[]
data
,
int
threadCount
)
{
List
<
PrimeCounterThread
>
summers
=
new
ArrayList
<>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
PrimeCounterThread
(
data
,
part
));
}
for
(
PrimeCounterThread
summerThread
:
summers
)
{
summerThread
.
start
();
}
for
(
PrimeCounterThread
summerThread
:
summers
)
{
try
{
summerThread
.
join
();
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Thread cannot join!"
);
}
}
long
numberOfPrimeNumbers
=
0
;
for
(
PrimeCounterThread
summerThread
:
summers
)
{
// System.out.println(summerThread.getPrimeNumbers());
numberOfPrimeNumbers
+=
summerThread
.
getPrimeNumbers
();
}
return
numberOfPrimeNumbers
;
}
}
src/main/java/parallelCounters/ParallelCounter1.java
0 → 100644
View file @
ad2914d6
package
parallelCounters
;
import
java.util.ArrayList
;
import
java.util.List
;
import
primeCounters.PrimeCounterRunnable
;
import
worker.WorkPartitioner
;
/**
* This class calculates the sum of an array using {@link PrimeCounterRunnable} and conventional thread
* start and join mechanisms placed into {@link PrimeCounterRunnable}.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
ParallelCounter1
{
public
static
long
findPrimeNumbers
(
int
[]
data
,
int
threadCount
)
{
List
<
PrimeCounterRunnable
>
summers
=
new
ArrayList
<>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
PrimeCounterRunnable
(
data
,
part
));
}
for
(
PrimeCounterRunnable
SummerRunnable
:
summers
)
{
SummerRunnable
.
startThread
();
}
for
(
PrimeCounterRunnable
SummerRunnable
:
summers
)
{
try
{
SummerRunnable
.
joinThread
();
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Thread cannot join!"
);
}
}
long
numberOfPrimeNumbers
=
0
;
for
(
PrimeCounterRunnable
summerRunnable
:
summers
)
{
numberOfPrimeNumbers
+=
summerRunnable
.
getPrimeNumbers
();
}
return
numberOfPrimeNumbers
;
}
}
src/main/java/parallelCounters/ParallelCounter2.java
0 → 100644
View file @
ad2914d6
package
parallelCounters
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
primeCounters.PrimeCounterThread
;
import
worker.WorkPartitioner
;
/**
* This class calculates the sum of an array using {@link PrimeCounterThread} and {@link ExecutorService}
* with a fixed thread pool. The class executes the threads one bye one.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
ParallelCounter2
{
public
static
long
findPrimeNumbers
(
int
[]
data
,
int
threadCount
)
{
List
<
PrimeCounterThread
>
summers
=
new
ArrayList
<>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
PrimeCounterThread
(
data
,
part
));
}
//creating a pool of 5 threads
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
for
(
PrimeCounterThread
summer:
summers
)
{
executor
.
execute
(
summer
);
}
executor
.
shutdown
();
//Waiting for all the thread to finalize
while
(!
executor
.
isTerminated
())
{
//System.out.println("Processing....");
}
return
summers
.
stream
().
mapToLong
(
PrimeCounterThread:
:
getPrimeNumbers
).
sum
();
}
}
src/main/java/parallelCounters/ParallelCounter3.java
0 → 100644
View file @
ad2914d6
package
parallelCounters
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
primeCounters.PrimeCounterCallable
;
import
worker.WorkPartitioner
;
/**
* This class calculates the sum of an array using {@link PrimeCounterCallable} and {@link ExecutorService}
* with a fixed thread pool. The class invokes all the callables at once.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
ParallelCounter3
{
public
static
long
findPrimeNumbers
(
int
[]
data
,
int
threadCount
)
{
//creating a pool of #threadCount threads
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
List
<
PrimeCounterCallable
>
summers
=
new
ArrayList
<
PrimeCounterCallable
>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
PrimeCounterCallable
(
data
,
part
));
}
List
<
Future
<
Long
>>
results
;
try
{
results
=
executor
.
invokeAll
(
summers
);
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Cannot invoke the threads."
);
return
-
1
;
}
executor
.
shutdown
();
long
numberOfPrimeNumbers
=
0
;
for
(
Future
<
Long
>
future
:
results
)
{
try
{
numberOfPrimeNumbers
+=
future
.
get
();
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
System
.
err
.
println
(
"Cannot get the results from threads."
);
return
-
2
;
}
}
return
numberOfPrimeNumbers
;
}
}
src/main/java/parallelCounters/ParallelCounter4.java
0 → 100644
View file @
ad2914d6
package
parallelCounters
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
primeCounters.PrimeCounterCallable
;
import
primeCounters.PrimeCounterThread
;
import
worker.WorkPartitioner
;
/**
* This class calculates the sum of an array using {@link PrimeCounterThread} and {@link ExecutorService}
* with a fixed thread pool. The class submits the threads one bye one for executions.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
ParallelCounter4
{
public
static
long
findPrimeNumbers
(
int
[]
data
,
int
threadCount
)
{
//creating a pool of 5 threads
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
List
<
Future
<
Long
>>
results
=
new
ArrayList
<
Future
<
Long
>>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
results
.
add
(
executor
.
submit
(
new
PrimeCounterCallable
(
data
,
part
)));
}
executor
.
shutdown
();
while
(!
executor
.
isTerminated
())
{
//System.out.println("Processing...");
}
long
numberOfPrimeNumbers
=
0
;
for
(
Future
<
Long
>
future
:
results
)
{
try
{
numberOfPrimeNumbers
+=
future
.
get
();
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
System
.
err
.
println
(
"Cannot get the results from threads."
);
return
-
2
;
}
}
return
numberOfPrimeNumbers
;
}
}
\ No newline at end of file
src/main/java/primeCounters/PrimeCounter.java
0 → 100644
View file @
ad2914d6
package
primeCounters
;
import
worker.WorkPartitioner.Part
;
import
worker.Worker
;
/**
* Base summer/worker class to calculate the sum of an array.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
PrimeCounter
extends
Worker
{
protected
long
numberOfPrimeNumbers
;
public
PrimeCounter
(
int
[]
data
,
Part
part
)
{
super
(
data
,
part
);
}
public
long
getPrimeNumbers
()
{
return
numberOfPrimeNumbers
;
}
}
src/main/java/primeCounters/PrimeCounterCallable.java
0 → 100644
View file @
ad2914d6
package
primeCounters
;
import
java.util.concurrent.Callable
;
import
primeChecker.PrimeChecker
;
import
worker.WorkPartitioner.Part
;
/**
* {@link Callable} based {@link PrimeCounter ).
*
* @author Ahmet Cengizhan Dirican
* @see https://github.com/acdirican
*/
public
class
PrimeCounterCallable
extends
PrimeCounter
implements
Callable
<
Long
>
{
PrimeChecker
primeChecker
=
new
PrimeChecker
();
public
PrimeCounterCallable
(
int
[]
data
,
Part
part
)
{
super
(
data
,
part
);
}
@Override
public
Long
call
()
throws
Exception
{
numberOfPrimeNumbers
=
0
;
for
(
int
i
=
start
;
i
<
finish
;
i
++)
{
if
(
primeChecker
.
isPrime
(
data
[
i
]))
numberOfPrimeNumbers
+=
1
;
}
return
numberOfPrimeNumbers
;
}
}
\ No newline at end of file
src/main/java/primeCounters/PrimeCounterRunnable.java
0 → 100644
View file @
ad2914d6
package
primeCounters
;
import
primeChecker.PrimeChecker
;
import
worker.WorkPartitioner.Part
;
/**
* {@link Runnable} based {@link PrimeCounter ).
*
* @author Ahmet Cengizhan Dirican
* @see https://github.com/acdirican
*/
public
class
PrimeCounterRunnable
extends
PrimeCounter
implements
Runnable
{
PrimeChecker
primeChecker
=
new
PrimeChecker
();
private
Thread
thread
;
public
PrimeCounterRunnable
(
int
[]
data
,
Part
part
)
{
super
(
data
,
part
);
}
@Override
public
void
run
()
{
numberOfPrimeNumbers
=
0
;
for
(
int
i
=
start
;
i
<
finish
;
i
++)
{
if
(
primeChecker
.
isPrime
(
data
[
i
]))
numberOfPrimeNumbers
+=
1
;
}
}
public
void
startThread
()
{
thread
=
new
Thread
(
this
);
thread
.
start
();
}
public
void
joinThread
()
throws
InterruptedException
{
thread
.
join
();
}
}
\ No newline at end of file
src/main/java/primeCounters/PrimeCounterThread.java
0 → 100644
View file @
ad2914d6
package
primeCounters
;
import
primeChecker.PrimeChecker
;
import
worker.WorkPartitioner.Part
;
/**
* {@link Thread} based summer/worker Because this class cannot extends the {@link PrimeCounter},
* it had to include the similar code.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
PrimeCounterThread
extends
Thread
{
PrimeChecker
primeChecker
=
new
PrimeChecker
();
private
int
start
;
private
int
finish
;
private
int
[]
data
;
private
long
numberOfPrimeNumbers
;
public
PrimeCounterThread
(
int
[]
data
,
int
start
,
int
finish
)
{
this
.
start
=
start
;
this
.
finish
=
finish
;
this
.
data
=
data
;
}
public
PrimeCounterThread
(
int
[]
data
,
Part
part
)
{
this
(
data
,
part
.
getStart
(),
part
.
getFinish
());
}
@Override
public
void
run
()
{
numberOfPrimeNumbers
=
0
;
for
(
int
i
=
start
;
i
<
finish
;
i
++)
{
if
(
primeChecker
.
isPrime
(
data
[
i
]))
numberOfPrimeNumbers
+=
1
;
}
}
public
long
getPrimeNumbers
()
{
return
numberOfPrimeNumbers
;
}
}
\ No newline at end of file
src/main/java/worker/WorkPartitioner.java
0 → 100644
View file @
ad2914d6
package
worker
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* This class paritition a given work into part. It a calculates the work parts start and finish indexes.
*
* @author Ahmet Cengizhan Dirican
*/
public
class
WorkPartitioner
{
public
static
List
<
Part
>
partitions
(
int
size
,
int
workerCount
){
List
<
Part
>
parts
=
new
ArrayList
<>();
int
part
=
(
int
)
Math
.
ceil
(
(
double
)
size
/
workerCount
);
for
(
int
i
=
0
;
i
<
workerCount
;
i
++)
{
int
start
=
i
*
part
;
int
finish
=
Math
.
min
((
i
+
1
)
*
part
,
size
);
parts
.
add
(
new
Part
(
start
,
finish
));
}
return
parts
;
}
public
static
class
Part
{
private
final
int
start
;
private
final
int
finish
;
public
Part
(
int
start
,
int
finish
)
{
this
.
start
=
start
;
this
.
finish
=
finish
;
}
public
int
getStart
()
{
return
start
;
}
public
int
getFinish
()
{
return
finish
;
}
}
}
src/main/java/worker/Worker.java
0 → 100644
View file @
ad2914d6
package
worker
;
import
worker.WorkPartitioner.Part
;
/**
* Base worker class for an array.
*
* @author Ahmet Cengizhan Dirican
*/
public
abstract
class
Worker
{
protected
final
int
start
;
protected
final
int
finish
;
protected
final
int
[]
data
;
public
Worker
(
int
[]
data
,
int
start
,
int
finish
)
{
this
.
start
=
start
;
this
.
finish
=
finish
;
this
.
data
=
data
;
}
public
Worker
(
int
[]
data
,
Part
part
)
{
this
(
data
,
part
.
getStart
(),
part
.
getFinish
());
}
public
int
getStart
()
{
return
start
;
}
public
int
getFinish
()
{
return
finish
;
}
public
int
[]
getData
()
{
return
data
;
}
}
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