Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
ParallelComputing
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
saad.aswad
ParallelComputing
Commits
c57c1035
Commit
c57c1035
authored
Nov 02, 2025
by
saad.aswad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Final file, for submission.
parent
6d516c99
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
12 deletions
+112
-12
Main.java
src/Main.java
+112
-12
No files found.
src/Main.java
View file @
c57c1035
import
java.util.Random
;
public
class
Main
{
public
static
final
int
MAX_PASSWORD
=
9999
;
public
static
void
main
(
String
[]
args
)
{
Random
random
=
new
Random
();
int
password
=
random
.
nextInt
(
MAX_PASSWORD
+
1
);
Vault
vault
=
new
Vault
(
password
);
System
.
out
.
println
(
"Cops on the way !!!"
);
System
.
out
.
println
(
"Password is: "
+
password
);
Thread
ascendingHacker
=
new
AscendingHackerThread
(
vault
);
ascendingHacker
.
setName
(
"AscendingHacker"
);
Thread
descendingHacker
=
new
DescendingHackerThread
(
vault
);
descendingHacker
.
setName
(
"DescendingHacker"
);
Thread
police
=
new
PoliceThread
();
police
.
setName
(
"Cops"
);
ascendingHacker
.
setPriority
(
Thread
.
MAX_PRIORITY
);
descendingHacker
.
setPriority
(
Thread
.
MAX_PRIORITY
);
ascendingHacker
.
start
();
descendingHacker
.
start
();
police
.
start
();
}
static
class
Vault
{
private
int
password
;
public
Vault
(
int
password
)
{
this
.
password
=
password
;
}
public
boolean
isCorrectPassword
(
int
guess
)
{
try
{
Thread
.
sleep
(
5
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
return
guess
==
this
.
password
;
}
}
static
abstract
class
HackerThread
extends
Thread
{
protected
Vault
vault
;
Thread
th
=
new
Thread
(){
@Override
public
void
run
(){
System
.
out
.
println
(
"Now doing the thread"
);
System
.
out
.
println
(
Thread
.
currentThread
().
getName
()
+
" Priority "
+
Thread
.
currentThread
().
getPriority
());
public
HackerThread
(
Vault
vault
)
{
this
.
vault
=
vault
;
}
@Override
public
void
run
()
{
hackVault
();
}
protected
abstract
void
hackVault
();
protected
void
win
(
int
guess
)
{
System
.
out
.
println
(
"!!! "
+
this
.
getName
()
+
" nailed it password was: "
+
guess
+
" !!!"
);
System
.
exit
(
0
);
}
}
static
class
AscendingHackerThread
extends
HackerThread
{
public
AscendingHackerThread
(
Vault
vault
)
{
super
(
vault
);
}
@Override
protected
void
hackVault
()
{
for
(
int
guess
=
0
;
guess
<=
MAX_PASSWORD
;
guess
++)
{
if
(
vault
.
isCorrectPassword
(
guess
))
{
win
(
guess
);
}
}
};
}
}
static
class
DescendingHackerThread
extends
HackerThread
{
public
DescendingHackerThread
(
Vault
vault
)
{
super
(
vault
);
}
@Override
protected
void
hackVault
()
{
for
(
int
guess
=
MAX_PASSWORD
;
guess
>=
0
;
guess
--)
{
if
(
vault
.
isCorrectPassword
(
guess
))
{
win
(
guess
);
}
}
}
}
th
.
setName
(
"New Thread"
);
th
.
setPriority
(
Thread
.
MAX_PRIORITY
);
System
.
out
.
println
(
"Thread will start"
);
th
.
start
();
System
.
out
.
println
(
"Thread has ended"
);
static
class
PoliceThread
extends
Thread
{
@Override
public
void
run
()
{
for
(
int
i
=
10
;
i
>
0
;
i
--)
{
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
i
+
" Seconds left.."
);
}
System
.
out
.
println
(
">>> Time is up <<<"
);
System
.
exit
(
0
);
}
}
}
\ No newline at end of file
}
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