Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Theory code Added
Browse files Browse the repository at this point in the history
  • Loading branch information
thewillyhuman committed Mar 12, 2017
1 parent b9b0297 commit 13b031e
Show file tree
Hide file tree
Showing 403 changed files with 63,835 additions and 0 deletions.
65 changes: 65 additions & 0 deletions theoryCode/concurrency/deadlock/accounts.with.deadlock/Account.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TPP.Concurrency.Deadlock {

public class Account {
private decimal balance;
private string accountNumber;

public Account(string accountNumber, decimal balance = 0) {
this.balance = balance;
this.accountNumber = accountNumber;
}

public string AccountNumber { get { return this.accountNumber; } }

/// <summary>
/// Withdraw from the account
/// <param name="amount">The amount of money to be withdrew</param>
/// <returns>Whether the transaction was successful</returns>
/// </summary>
public bool Withdraw(decimal amount) {
if (this.balance < amount)
return false;
this.balance -= amount;
return true;
}

/// <summary>
/// Credit in the account
/// <param name="amount">The amount of money to be credited in the account</param>
/// </summary>
public void Credit(decimal amount) {
this.balance += amount;
}


/// <summary>
/// Transfer money from the current account (this) to the destination parameter
/// <param name="destination">The detination account where the money is going to be transferred to</param>
/// <param name="amount">The amount of many to be transferred</param>
/// <returns>Whether the transaction was successfully done</returns>
/// </summary>
public bool Transfer(Account destination, decimal amount) {
lock (this) {
lock (destination) {
Thread.Sleep(100); // Simulates the processing...
if (this.Withdraw(amount)) {
destination.Credit(amount);
return true;
}
else
return false;
}
}
}




}
}
41 changes: 41 additions & 0 deletions theoryCode/concurrency/deadlock/accounts.with.deadlock/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TPP.Concurrency.Deadlock {

class Program {

private static void Transfer(Account accountA, Account accountB, decimal amount) {
Console.WriteLine("Transferring {0:N} euros from account {1} to account {2}...",
amount, accountA.AccountNumber, accountB.AccountNumber);
if (accountA.Transfer(accountB, amount))
Console.WriteLine("\tSuccessful transaction, thread {0}.", Thread.CurrentThread.Name);
else
Console.WriteLine("\tWrong transaction, thread {0}.", Thread.CurrentThread.Name);
}

public static void Main() {
decimal initialAmount = 30000;
Account accountA = new Account("A", initialAmount), accountB = new Account("B", initialAmount);

Random random = new Random();
int iterations = 10;
Thread[] threads = new Thread[iterations];
for (int i = 0; i < iterations; i++) {
decimal amount = (decimal)random.NextDouble() * random.Next(1, 600);
if (i % 2 == 0)
threads[i] = new Thread(() => Transfer(accountA, accountB, amount));
else
threads[i] = new Thread(() => Transfer(accountB, accountA, amount));
threads[i].Name = "Transfer number " + i;
}

foreach (Thread thread in threads)
thread.Start();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("accounts.with.deadlock")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("accounts.with.deadlock")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("620a1ee4-ca1b-4d26-853a-f0e36a940ba1")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A4C73E4F-4B33-4533-8D97-3D41E8F2A22C}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>accounts.with.deadlock</RootNamespace>
<AssemblyName>accounts.with.deadlock</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Account.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TPP.Concurrency.Deadlock {

public class Account {
private decimal balance;
private string accountNumber;

public Account(string accountNumber, decimal balance = 0) {
this.balance = balance;
this.accountNumber = accountNumber;
}

public string AccountNumber { get { return this.accountNumber; } }

/// <summary>
/// Withdraw from the account
/// <param name="amount">The amount of money to be withdrew</param>
/// <returns>Whether the transaction was successful</returns>
/// </summary>
public bool Withdraw(decimal amount) {
lock (this) {
if (this.balance < amount)
return false;
this.balance -= amount;
return true;
}
}

/// <summary>
/// Credit in the account
/// <param name="amount">The amount of money to be credited in the account</param>
/// </summary>
public void Credit(decimal amount) {
lock (this) {
this.balance += amount;
}
}


/// <summary>
/// Transfer money from the current account (this) to the destination parameter
/// <param name="destination">The detination account where the money is going to be transferred to</param>
/// <param name="amount">The amount of many to be transferred</param>
/// <returns>Whether the transaction was successfully done</returns>
/// </summary>
public bool Transfer(Account destination, decimal amount) {
Thread.Sleep(100); // Simula procesamiento...
if (this.Withdraw(amount)) {
destination.Credit(amount);
return true;
}
else
return false;
}




}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TPP.Concurrency.Deadlock {

class Program {

private static void Transfer(Account accountA, Account accountB, decimal amount) {
Console.WriteLine("Transferring {0:N} euros from account {1} to account {2}...",
amount, accountA.AccountNumber, accountB.AccountNumber);
if (accountA.Transfer(accountB, amount))
Console.WriteLine("\tSuccessful transaction, thread {0}.", Thread.CurrentThread.Name);
else
Console.WriteLine("\tWrong transaction, thread {0}.", Thread.CurrentThread.Name);
}

public static void Main() {
decimal initialAmount = 30000;
Account accountA = new Account("A", initialAmount), accountB = new Account("B", initialAmount);

Random random = new Random();
int iterations = 10;
Thread[] threads = new Thread[iterations];
for (int i = 0; i < iterations; i++) {
decimal amount = (decimal)random.NextDouble() * random.Next(1, 600);
if (i % 2 == 0)
threads[i] = new Thread(() => Transfer(accountA, accountB, amount));
else
threads[i] = new Thread(() => Transfer(accountB, accountA, amount));
threads[i].Name = "Transfer number " + i;
}

foreach (Thread thread in threads)
thread.Start();
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("accounts.without.deadlock")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("accounts.without.deadlock")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7074fb38-cc23-4b56-9fa1-5af6ec6ec434")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 13b031e

Please sign in to comment.