Merge branch 'os-atomic' of github.com:kanmaytacker/fundamentals

This commit is contained in:
Tanmay 2022-08-26 13:21:22 +01:00
commit 129277bbed
18 changed files with 319 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
os/code/target/*

81
os/code/os/pom.xml Normal file
View File

@ -0,0 +1,81 @@
<?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>com.scaler</groupId>
<artifactId>os</artifactId>
<version>pyenv shell 3.9.7</version>
<name>os</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,13 @@
package com.scaler;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -0,0 +1,22 @@
package com.scaler.addersubtractor;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class Adder implements Runnable {
private Count count;
@Override
public void run() {
for (int i = 1; i <= 100; ++i) {
count.getValue().getAndAdd(i);
try {
Thread.sleep(10);
} catch (Exception e) {
System.out.println("Something wrong happened");
}
}
}
}

View File

@ -0,0 +1,15 @@
package com.scaler.addersubtractor;
import java.util.concurrent.atomic.AtomicInteger;
public class Count {
private AtomicInteger value = new AtomicInteger(0);
public AtomicInteger getValue() {
return value;
}
public void setValue(AtomicInteger value) {
this.value = value;
}
}

View File

@ -0,0 +1,27 @@
package com.scaler.addersubtractor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Runner {
public static void main(String[] args) {
Count count = new Count();
Adder adder = new Adder(count);
Subtractor subtractor = new Subtractor(count);
ExecutorService executor = Executors.newCachedThreadPool();
executor.execute(adder);
executor.execute(subtractor);
executor.shutdown();
try {
executor.awaitTermination(100, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Something wrong happened");
}
System.out.println(count.getValue());
}
}

View File

@ -0,0 +1,28 @@
package com.scaler.addersubtractor;
import java.util.concurrent.locks.Lock;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class Subtractor implements Runnable {
private Count count;
@Override
public void run() {
for (int i = 1; i <= 100; ++i) {
count.getValue().getAndAdd(-i);
try {
Thread.sleep(10);
} catch (Exception e) {
System.out.println("Something wrong happened");
}
for (int j = 0; j < 10000; ++j) {
// do something
}
}
}
}

View File

@ -0,0 +1,36 @@
package com.scaler.producerconsumer;
import java.util.Queue;
import java.util.concurrent.Semaphore;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class Consumer implements Runnable {
private Queue<UnitOfWork> store;
private String name;
private Semaphore forProducer;
private Semaphore forConsumer;
@Override
public void run() {
while (true) {
try {
forConsumer.acquire();
} catch (InterruptedException e) {
throw new RuntimeException("Error acquiring semaphore " + e);
}
store.remove();
System.out.println("Consumed: " + name + " Left units :" + store.size());
forProducer.release();
}
}
}

View File

@ -0,0 +1,36 @@
package com.scaler.producerconsumer;
import java.util.Queue;
import java.util.concurrent.Semaphore;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class Producer implements Runnable {
private Queue<UnitOfWork> store;
private int maxSize;
private String name;
private Semaphore forProducer;
private Semaphore forConsumer;
@Override
public void run() {
while (true) {
try {
forProducer.acquire();
} catch (InterruptedException e) {
throw new RuntimeException("Error acquiring semaphore " + e);
}
store.add(new UnitOfWork());
System.out.println("Produced: " + name + " Left units :" + store.size());
forConsumer.release();
}
}
}

View File

@ -0,0 +1,35 @@
package com.scaler.producerconsumer;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors;
public class Runner {
private static final Set<String> producerNames = Set.of("p1", "p2", "p3");
private static final Set<String> consumerNames = Set.of("c1", "c2", "c3", "c4");
public static void main(String[] args) {
Queue<UnitOfWork> store = new ConcurrentLinkedDeque<>();
int maxSize = 20;
Semaphore forProducer = new Semaphore(maxSize);
Semaphore forConsumer = new Semaphore(0);
Set<Producer> producers = producerNames
.stream()
.map(name -> new Producer(store, maxSize, name, forProducer, forConsumer))
.collect(Collectors.toSet());
Set<Consumer> consumers = consumerNames
.stream()
.map(name -> new Consumer(store, name, forProducer, forConsumer))
.collect(Collectors.toSet());
producers.forEach(producer -> new Thread(producer).start());
consumers.forEach(consumer -> new Thread(consumer).start());
}
}

View File

@ -0,0 +1,5 @@
package com.scaler.producerconsumer;
public class UnitOfWork {
}

View File

@ -0,0 +1,20 @@
package com.scaler;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

Binary file not shown.