-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Giorgi Lekveishvili
committed
Mar 27, 2020
0 parents
commit 2d5707d
Showing
13 changed files
with
105,307 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Account.java | ||
|
||
/* | ||
Simple, thread-safe Account class encapsulates | ||
a balance and a transaction count. | ||
*/ | ||
public class Account { | ||
private int id; | ||
private int balance; | ||
private int transactions; | ||
|
||
// It may work out to be handy for the account to | ||
// have a pointer to its Bank. | ||
// (a suggestion, not a requirement) | ||
private Bank bank; | ||
|
||
public Account(Bank bank, int id, int balance) { | ||
this.bank = bank; | ||
this.id = id; | ||
this.balance = balance; | ||
transactions = 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Bank.java | ||
|
||
/* | ||
Creates a bunch of accounts and uses threads | ||
to post transactions to the accounts concurrently. | ||
*/ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Bank { | ||
public static final int ACCOUNTS = 20; // number of accounts | ||
|
||
|
||
|
||
/* | ||
Reads transaction data (from/to/amt) from a file for processing. | ||
(provided code) | ||
*/ | ||
public void readFile(String file) { | ||
try { | ||
BufferedReader reader = new BufferedReader(new FileReader(file)); | ||
|
||
// Use stream tokenizer to get successive words from file | ||
StreamTokenizer tokenizer = new StreamTokenizer(reader); | ||
|
||
while (true) { | ||
int read = tokenizer.nextToken(); | ||
if (read == StreamTokenizer.TT_EOF) break; // detect EOF | ||
int from = (int)tokenizer.nval; | ||
|
||
tokenizer.nextToken(); | ||
int to = (int)tokenizer.nval; | ||
|
||
tokenizer.nextToken(); | ||
int amount = (int)tokenizer.nval; | ||
|
||
// Use the from/to/amount | ||
|
||
// YOUR CODE HERE | ||
} | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
System.exit(1); | ||
} | ||
} | ||
|
||
/* | ||
Processes one file of transaction data | ||
-fork off workers | ||
-read file into the buffer | ||
-wait for the workers to finish | ||
*/ | ||
public void processFile(String file, int numWorkers) { | ||
} | ||
|
||
|
||
|
||
/* | ||
Looks at commandline args and calls Bank processing. | ||
*/ | ||
public static void main(String[] args) { | ||
// deal with command-lines args | ||
if (args.length == 0) { | ||
System.out.println("Args: transaction-file [num-workers [limit]]"); | ||
System.exit(1); | ||
} | ||
|
||
String file = args[0]; | ||
|
||
int numWorkers = 1; | ||
if (args.length >= 2) { | ||
numWorkers = Integer.parseInt(args[1]); | ||
} | ||
|
||
// YOUR CODE HERE | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Buffer.java | ||
|
||
import java.util.concurrent.*; | ||
|
||
/* | ||
Holds the transactions for the worker | ||
threads. | ||
*/ | ||
public class Buffer { | ||
public static final int SIZE = 64; | ||
|
||
// YOUR CODE HERE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Cracker.java | ||
/* | ||
Generates SHA hashes of short strings in parallel. | ||
*/ | ||
|
||
import java.security.*; | ||
|
||
public class Cracker { | ||
// Array of chars used to produce strings | ||
public static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz0123456789.,-!".toCharArray(); | ||
|
||
|
||
/* | ||
Given a byte[] array, produces a hex String, | ||
such as "234a6f". with 2 chars for each byte in the array. | ||
(provided code) | ||
*/ | ||
public static String hexToString(byte[] bytes) { | ||
StringBuffer buff = new StringBuffer(); | ||
for (int i=0; i<bytes.length; i++) { | ||
int val = bytes[i]; | ||
val = val & 0xff; // remove higher bits, sign | ||
if (val<16) buff.append('0'); // leading 0 | ||
buff.append(Integer.toString(val, 16)); | ||
} | ||
return buff.toString(); | ||
} | ||
|
||
/* | ||
Given a string of hex byte values such as "24a26f", creates | ||
a byte[] array of those values, one byte value -128..127 | ||
for each 2 chars. | ||
(provided code) | ||
*/ | ||
public static byte[] hexToArray(String hex) { | ||
byte[] result = new byte[hex.length()/2]; | ||
for (int i=0; i<hex.length(); i+=2) { | ||
result[i/2] = (byte) Integer.parseInt(hex.substring(i, i+2), 16); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
|
||
public static void main(String[] args) { | ||
if (args.length < 2) { | ||
System.out.println("Args: target length [workers]"); | ||
System.exit(1); | ||
} | ||
// args: targ len [num] | ||
String targ = args[0]; | ||
int len = Integer.parseInt(args[1]); | ||
int num = 1; | ||
if (args.length>2) { | ||
num = Integer.parseInt(args[2]); | ||
} | ||
// a! 34800e15707fae815d7c90d49de44aca97e2d759 | ||
// xyz 66b27417d37e024c46526c2f6d358a754fc552f3 | ||
|
||
// YOUR CODE HERE | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// JCount.java | ||
|
||
/* | ||
Basic GUI/Threading exercise. | ||
*/ | ||
|
||
import javax.swing.*; | ||
import java.awt.event.*; | ||
|
||
public class JCount extends JPanel { | ||
public JCount() { | ||
// Set the JCount to use Box layout | ||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); | ||
|
||
// YOUR CODE HERE | ||
} | ||
|
||
static public void main(String[] args) { | ||
// Creates a frame with 4 JCounts in it. | ||
// (provided) | ||
JFrame frame = new JFrame("The Count"); | ||
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); | ||
|
||
frame.add(new JCount()); | ||
frame.add(new JCount()); | ||
frame.add(new JCount()); | ||
frame.add(new JCount()); | ||
|
||
frame.pack(); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
frame.setVisible(true); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Transaction.java | ||
/* | ||
(provided code) | ||
Transaction is just a dumb struct to hold | ||
one transaction. Supports toString. | ||
*/ | ||
public class Transaction { | ||
public int from; | ||
public int to; | ||
public int amount; | ||
|
||
public Transaction(int from, int to, int amount) { | ||
this.from = from; | ||
this.to = to; | ||
this.amount = amount; | ||
} | ||
|
||
public String toString() { | ||
return("from:" + from + " to:" + to + " amt:" + amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import java.io.*; | ||
import java.net.*; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import javax.swing.*; | ||
|
||
public class WebWorker extends Thread { | ||
/* | ||
This is the core web/download i/o code... | ||
download() { | ||
InputStream input = null; | ||
StringBuilder contents = null; | ||
try { | ||
URL url = new URL(urlString); | ||
URLConnection connection = url.openConnection(); | ||
// Set connect() to throw an IOException | ||
// if connection does not succeed in this many msecs. | ||
connection.setConnectTimeout(5000); | ||
connection.connect(); | ||
input = connection.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(input)); | ||
char[] array = new char[1000]; | ||
int len; | ||
contents = new StringBuilder(1000); | ||
while ((len = reader.read(array, 0, array.length)) > 0) { | ||
contents.append(array, 0, len); | ||
Thread.sleep(100); | ||
} | ||
// Successful download if we get here | ||
} | ||
// Otherwise control jumps to a catch... | ||
catch(MalformedURLException ignored) {} | ||
catch(InterruptedException exception) { | ||
// YOUR CODE HERE | ||
// deal with interruption | ||
} | ||
catch(IOException ignored) {} | ||
// "finally" clause, to close the input stream | ||
// in any case | ||
finally { | ||
try{ | ||
if (input != null) input.close(); | ||
} | ||
catch(IOException ignored) {} | ||
} | ||
*/ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
http://www.stanford.edu/ | ||
http://www.stanford.edu/class/cs108/ | ||
http://news.yahoo.com/ | ||
http://www.google.com/ | ||
http://www.ibm.com/ | ||
http://www.dell.com/ | ||
http://www.microsoft.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
0 1 1 | ||
2 3 1 | ||
4 5 1 | ||
6 7 1 | ||
8 9 1 | ||
10 11 1 | ||
12 13 1 | ||
14 15 1 | ||
16 17 1 | ||
18 19 1 |