Skip to content

Commit

Permalink
HW04
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi Lekveishvili committed Mar 27, 2020
0 parents commit 2d5707d
Show file tree
Hide file tree
Showing 13 changed files with 105,307 additions and 0 deletions.
100,000 changes: 100,000 additions & 0 deletions 100k.txt

Large diffs are not rendered by default.

5,000 changes: 5,000 additions & 0 deletions 5k.txt

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions Account.java
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;
}

}
80 changes: 80 additions & 0 deletions Bank.java
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
}
}

13 changes: 13 additions & 0 deletions Buffer.java
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
}
62 changes: 62 additions & 0 deletions Cracker.java
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 added HW04Threads.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions JCount.java
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);
}
}

21 changes: 21 additions & 0 deletions Transaction.java
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);
}
}
55 changes: 55 additions & 0 deletions WebWorker.java
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) {}
}
*/

}
1 change: 1 addition & 0 deletions links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://www.stanford.edu/class/cs108/foo.txthttp://www.stanford.edu/class/cs108/foo2.txthttp://www.stanford.edu/class/cs106a/http://www.stanford.edu/class/cs106b/http://www.stanford.edu/class/cs107/http://www.stanford.edu/class/cs108/bar.txthttp://www.stanford.edu/class/cs108/baz.txthttp://www.stanford.edu/class/cs108/ptth/:/syntaxerror.com/http://psych.stanford.edu/http://nifty.stanford.edu/http://transfer.stanford.edu/http://www.stanford.edu/class/cs108/foo3.txthttp://www.stanford.edu/class/cs108/foo4.txthttp://www.stanford.edu/class/cs108/foo5.txthttp://www.stanford.edu/class/cs108/foo6.txt
Expand Down
7 changes: 7 additions & 0 deletions links2.txt
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/
10 changes: 10 additions & 0 deletions small.txt
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

0 comments on commit 2d5707d

Please sign in to comment.