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,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;
}
}
}