Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
Lab2-Examples
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
Michele Fiori
Lab2-Examples
Commits
0f6799fa
Commit
0f6799fa
authored
Apr 07, 2024
by
Michele Fiori
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Switched comments to english
parent
956ddbc7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
14 additions
and
14 deletions
+14
-14
.DS_Store
.DS_Store
+0
-0
compiler.xml
.idea/compiler.xml
+1
-1
misc.xml
.idea/misc.xml
+1
-1
Main.java
src/main/java/join/Main.java
+3
-3
Main.java
src/main/java/semaphore/Main.java
+2
-2
MyThread.java
src/main/java/semaphore/MyThread.java
+1
-1
Semaphore.java
src/main/java/semaphore/Semaphore.java
+6
-6
No files found.
.DS_Store
View file @
0f6799fa
No preview for this file type
.idea/compiler.xml
View file @
0f6799fa
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"CompilerConfiguration"
>
<bytecodeTargetLevel
target=
"1
1
"
/>
<bytecodeTargetLevel
target=
"1
5
"
/>
</component>
</project>
\ No newline at end of file
.idea/misc.xml
View file @
0f6799fa
...
...
@@ -4,7 +4,7 @@
<component
name=
"FrameworkDetectionExcludesConfiguration"
>
<file
type=
"web"
url=
"file://$PROJECT_DIR$"
/>
</component>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1
1
"
project-jdk-name=
"1.8"
project-jdk-type=
"JavaSDK"
>
<component
name=
"ProjectRootManager"
version=
"2"
languageLevel=
"JDK_1
5"
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
src/main/java/join/Main.java
View file @
0f6799fa
...
...
@@ -9,19 +9,19 @@ public class Main {
public
static
void
main
(
String
arg
[])
throws
Exception
{
Random
r
=
new
Random
();
ArrayList
<
Thread
>
threads
=
new
ArrayList
<
Thread
>();
//
c
reate some threads
//
C
reate some threads
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
MyThread
mt
=
new
MyThread
(
r
);
threads
.
add
(
mt
);
}
System
.
out
.
println
(
"All threads have been created."
);
//
s
tart all the threads
//
S
tart all the threads
for
(
Thread
t:
threads
)
{
t
.
start
();
}
System
.
out
.
println
(
"All threads have been started."
);
System
.
out
.
println
(
"Start waiting for all thread to finish..."
);
//
w
ait all the thread to finish
//
W
ait all the thread to finish
for
(
Thread
t
:
threads
)
{
t
.
join
();
}
...
...
src/main/java/semaphore/Main.java
View file @
0f6799fa
...
...
@@ -8,13 +8,13 @@ public class Main {
Random
r
=
new
Random
();
ArrayList
<
Thread
>
threads
=
new
ArrayList
<
Thread
>();
Semaphore
s
=
new
Semaphore
(
4
);
//
c
reate some threads
//
C
reate some threads
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
MyThread
mt
=
new
MyThread
(
r
,
i
,
s
);
threads
.
add
(
mt
);
}
//
s
tart all the threads
//
S
tart all the threads
for
(
Thread
t:
threads
)
{
t
.
start
();
}
...
...
src/main/java/semaphore/MyThread.java
View file @
0f6799fa
...
...
@@ -18,7 +18,7 @@ public class MyThread extends Thread {
System
.
out
.
println
(
"Thread "
+
id
+
" wants to enter in the critical region"
);
sem
.
enter
();
System
.
out
.
println
(
"Thread "
+
id
+
" entered in the critical region!"
);
wasteSomeTime
();
//
i
t takes some times to compleate the work in the critical region
wasteSomeTime
();
//
I
t takes some times to compleate the work in the critical region
System
.
out
.
println
(
"Thread "
+
id
+
" is going to get out from the critical region"
);
sem
.
exit
();
}
//end run
...
...
src/main/java/semaphore/Semaphore.java
View file @
0f6799fa
package
semaphore
;
//Class
e che implementa tramite wait e notify un Semaforo.
//
Se non vi ricordate cosa sia un semaforo, g
oogle is your best friend.
package
semaphore
;
//Class
that implements a semaphore, thanks to wait() and notify()
//
If you need to recap what a semaphore is, G
oogle is your best friend.
public
class
Semaphore
{
private
int
maxNumber
;
//
numero massimo di thread
private
int
threadsIn
;
//
conteggio dei thread nell'area critica
private
int
maxNumber
;
//
Maximum number of threads
private
int
threadsIn
;
//
Number of threads in the critical region
Semaphore
(
int
max
)
{
maxNumber
=
max
;
...
...
@@ -12,7 +12,7 @@ public class Semaphore {
public
synchronized
void
enter
()
{
System
.
out
.
println
(
""
+
threadsIn
+
" in the critical region..."
);
//
quando abbiamo raggiunto il numero massimo di thread, chi vuole entrare aspetta
//
When we reach the maximum number of threads, new threads need to wait
while
(
threadsIn
>=
maxNumber
)
{
try
{
this
.
wait
();}
catch
(
InterruptedException
ie
)
{
ie
.
printStackTrace
();}
...
...
@@ -23,7 +23,7 @@ public class Semaphore {
public
synchronized
void
exit
()
{
threadsIn
--;
//
quando un thread esce dall'area critica, sveglia qualcuno in attesa di entrare (se present
e)
//
Whena thread exits the critical region, it awakens another thread that is waiting (if there is on
e)
this
.
notify
();
}
}
...
...
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