Implement Answers.md
This commit is contained in:
Generated
+10
@@ -0,0 +1,10 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
Generated
+13
@@ -0,0 +1,13 @@
|
||||
<?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="HW-09-Advanced-Multithreading" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?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>
|
||||
Generated
+20
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<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>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://maven.myket.ir/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+12
@@ -0,0 +1,12 @@
|
||||
<?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" project-jdk-name="25" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
## Question 1
|
||||
An atomic variable in Computer Science refers to a basic data or input variable that is used to build performance variables. These variables are not summaries or ratios, but rather fundamental building blocks in operational systems.
|
||||
|
||||
Atomic variables allow multiple threads to safely read and update a shared value without using explicit locks, guaranteeing that operations like increment-and-update happen as a single, uninterruptible step. Ordinary variables don't provide this guarantee—if multiple threads modify them concurrently, updates can be lost due to race conditions.
|
||||
|
||||
## Question 2
|
||||
`AtomicInteger`
|
||||
|
||||
`AtomicLong`
|
||||
|
||||
`AtomicBoolean`
|
||||
|
||||
`AtomicReference<T>` for any type of object.
|
||||
|
||||
## Question 3
|
||||
| | Locks (`synchronized`/`ReentrantLock`) | Atomic Variables |
|
||||
|---|---|---|
|
||||
| Mechanism | Blocking (mutual exclusion) | Lock-free |
|
||||
| Scope | Can protect multiple statements/variables | Single variable only |
|
||||
| Performance | Slower under contention | Generally faster |
|
||||
| Deadlock risk | Possible | None |
|
||||
| Best for | Complex critical sections | Simple counters, flags, single values |
|
||||
|
||||
## Question 4
|
||||
A program can be completely free of race conditions yet still perform poorly, because the very mechanisms used to guarantee correctness — such as locks or CAS — introduce overhead. When many threads compete for the same shared resource, they end up **blocking or repeatedly retrying**, which sharply reduces throughput even though correctness is fully preserved.
|
||||
|
||||
Some concurrency-related factors that may limit scalability even when correctness iss guaranteed:
|
||||
|
||||
1. Lock contention
|
||||
2. Context switching overhead
|
||||
3. Cache coherence traffic
|
||||
|
||||
## Question 5
|
||||
Despite the three factors given in the previous question there is a vital factor which is **limited CPU cores**.
|
||||
|
||||
once threads exceed available cores, they compete for the same processing units, adding scheduling overhead instead of true parallelism.
|
||||
|
||||
Context-switching overhead – the OS spends more time switching between threads than executing actual work.
|
||||
|
||||
Cache coherence traffic – shared/false-shared memory locations cause costly cross-core cache invalidation.
|
||||
|
||||
## Question 6
|
||||
A precise timing where two or more threads each acquire one lock and then attempt to acquire the other's lock simultaneously.
|
||||
|
||||
1. Stress testing with high concurrency – run many more threads than in normal testing.
|
||||
2. Deliberate interleaving control / thread scheduling tools – use tools or techniques that artificially manipulate thread timing to force specific interleavings, such as:
|
||||
Inserting `Thread.sleep()` or `yield()` calls strategically between lock acquisitions.
|
||||
Reference in New Issue
Block a user