This commit is contained in:
2024-02-09 14:44:07 -06:00
parent eb94a026a7
commit bcb6aa480a
25 changed files with 1112 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<?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>edu.slcc.asdv.caleb</groupId>
<artifactId>Threads_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.threads_calebfontenot.Threads_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.threads_calebfontenot;
/**
*
* @author caleb
*/
public class Threads_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,62 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author caleb
*/
public class AccountNoSync {
private static Account account = new Account();
public static void main(String[] args)
{
ExecutorService executor = Executors.newCachedThreadPool();
// Create and launch 100 threads
for (int i = 0; i < 100; ++i) {
executor.execute(new AddAPennyTask());
}
executor.shutdown();
while (!executor.isTerminated());
System.out.println("Balance is " + account.getBalance());
}
// This is a thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run()
{
account.deposit(1);
}
}
// An inner class for account
private static class Account {
private int balance = 0;
private int getBalance()
{
return balance;
}
public void deposit(int amount)
{
int newBalance = balance + amount;
// This delay is deliberately added to magnify the data corruption and makes it easy to see...
//try {
//Thread.sleep(3);
//} catch (InterruptedException ex) {
//}
balance = newBalance;
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author caleb
*/
public class AccountSync2 {
private static Account account = new Account();
public static void main(String[] args)
{
ExecutorService executor = Executors.newCachedThreadPool();
// Create and launch 100 threads
for (int i = 0; i < 100; ++i) {
executor.execute(new AddAPennyTask());
}
executor.shutdown();
while (!executor.isTerminated());
System.out.println("Balance is " + account.getBalance());
}
// This is a thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run()
{
account.deposit(1);
}
}
// An inner class for account
private static class Account {
private int balance = 0;
private int getBalance()
{
return balance;
}
public void deposit(int amount)
{
int newBalance = balance + amount;
// This delay is deliberately added to magnify the data corruption and makes it easy to see...
try {
Thread.sleep(3);
} catch (InterruptedException ex) {
}
balance = newBalance;
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author caleb
*/
public class AccountSync3 {
private static Lock lock = new ReentrantLock(); // Create a lock
private static Account account = new Account();
public static void main(String[] args)
{
ExecutorService executor = Executors.newCachedThreadPool();
// Create and launch 100 threads
for (int i = 0; i < 100; ++i) {
executor.execute(new AddAPennyTask());
}
executor.shutdown();
while (!executor.isTerminated());
System.out.println("Balance is " + account.getBalance());
}
// This is a thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run()
{
account.deposit(1);
}
}
// An inner class for account
private static class Account {
private int balance = 0;
private int getBalance()
{
return balance;
}
public void deposit(int amount)
{
lock.lock(); // Acquire the lock
try {
int newBalance = balance + amount;
// This delay is deliberately added to magnify the data corruption and makes it easy to see...
//try {
//Thread.sleep(3);
//} catch (InterruptedException ex) {
//}
balance = newBalance;
} finally {
lock.unlock();
}
}
}
}

View File

@@ -0,0 +1,62 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author caleb
*/
public class AccountSyncronized {
private static Account account = new Account();
public static void main(String[] args)
{
ExecutorService executor = Executors.newCachedThreadPool();
// Create and launch 100 threads
for (int i = 0; i < 100; ++i) {
executor.execute(new AddAPennyTask());
}
executor.shutdown();
while (!executor.isTerminated());
System.out.println("Balance is " + account.getBalance());
}
// This is a thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run()
{
account.deposit(1);
}
}
// An inner class for account
private static class Account {
private int balance = 0;
private int getBalance()
{
return balance;
}
public synchronized void deposit(int amount)
{
int newBalance = balance + amount;
// This delay is deliberately added to magnify the data corruption and makes it easy to see...
try {
Thread.sleep(3);
} catch (InterruptedException ex) {
}
balance = newBalance;
}
}
}

View File

@@ -0,0 +1,13 @@
edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.class
sync/AccountSync3$Account.class
sync/AccountSync2.class
sync/AccountSync2$AddAPennyTask.class
sync/AccountSync2$Account.class
sync/AccountSyncronized$AddAPennyTask.class
sync/AccountSync3$AddAPennyTask.class
sync/AccountSync3.class
sync/AccountNoSync.class
sync/AccountSyncronized.class
sync/AccountSyncronized$Account.class
sync/AccountNoSync$Account.class
sync/AccountNoSync$AddAPennyTask.class

View File

@@ -0,0 +1,5 @@
/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync2.java
/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.java
/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSyncronized.java
/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync3.java
/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountNoSync.java