Commit 8971339c authored by Chris Gouveia's avatar Chris Gouveia
Browse files

Added Example Code

parent a15171d0
Loading
Loading
Loading
Loading
+7 −0
Changes for examples/Chapter 1/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+27 −0
Changes for examples/Chapter 2/buggy/Allocy.java: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
class ResponseBuilder {
        private String response;

        public ResponseBuilder(String template) {
                response = template;
        }

        public void addLine(String line) {
                response += line;
        }

        public String getResponse() {
                return response;
        }
}

class Allocy {
        public static void main(String[] args) {
                while (true) {
                        ResponseBuilder rb = new ResponseBuilder("Result:");
                        for (int i = 0; i < 100000; ++i) {
                                rb.addLine("#" + i + ": OK");
                        }
                        rb.addLine("End of response.");
                }
        }
}
+37 −0
Changes for examples/Chapter 2/buggy/Collecty.java: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
class DataFetcher {
        public void fetchData(String datum, int index) {
                if (index == 0 || index % 37 != 0) {
                        processIt();
                        return;
                }
                System.out.println("Error fetching data, cleaning up.");
                System.gc();
        }

        private void processIt() {
        }
}

class RequestProcessor {
        private DataFetcher fetcher;

        public RequestProcessor() {
                fetcher = new DataFetcher();
        }

        public void processRequest(String url, int index) throws Exception {
                if (index != 0 && index % 43 == 0)
                        fetcher.fetchData(url, index);
                else
                        Thread.sleep(1);
        }
}

class Collecty {
        public static void main(String[] args) throws Exception {
                RequestProcessor rp = new RequestProcessor();
                for (int i = 0; ; ++i) {
                        rp.processRequest("/api/all", i);
                }
        }
}
+70 −0
Changes for examples/Chapter 2/buggy/Computey.java: 70 added lines, 0 removed lines.
Original line number Diff line number Diff line
import java.util.ArrayList;

class Primes {
        private final int from;
        private final int to;
        private final int numThreads;
        private ArrayList<Integer> primes = new ArrayList<>();
        private Object primesLock = new Object();

        public Primes(int from, int to, int numThreads) {
                this.from = from;
                this.to = to;
                this.numThreads = numThreads;
        }

        public int count() {
                return primes.size();
        }

        public void execute() throws Exception {
                Thread[] threads = new Thread[numThreads];
                int chunkSize = (to-from)/numThreads;
                for (int i = 0; i < numThreads; ++i) {
                        final int myStart = i*chunkSize;
                        final int myEnd = (i+1)*chunkSize - 1;
                        threads[i] = new Thread(new Runnable() {
                                public void run() {
                                        primesThread(myStart, myEnd);
                                }
                        });
                        threads[i].start();
                }
                for (Thread thread : threads) {
                        thread.join();
                }
        }

        private void primesThread(int from, int to) {
                for (int i = from; i <= to; ++i) {
                        if (isPrime(i)) {
                                synchronized (primesLock) {
                                        primes.add(i);
                                }
                        }
                }
        }

        private static boolean isPrime(int number) {
                // Obviously very inefficient
                if (number == 2) return true;
                if (number % 2 == 0) return false;
                for (int i = 3; i < number; i += 2)
                        if (number % i == 0)
                                return false;
                return true;
        }
}

class Computey {
        public static void main(String[] args) throws Exception {
                int from = Integer.parseInt(args[0]);
                int to = Integer.parseInt(args[1]);
                int numThreads = Integer.parseInt(args[2]);
                Primes primes = new Primes(from, to, numThreads);
                System.out.println("Press RETURN to start.");
                System.in.read();
                primes.execute();
                System.out.println("Found " + primes.count() + " primes");
        }
}
+50 −0
Changes for examples/Chapter 2/buggy/Makefile: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
JAVAFLAGS=-XX:+PreserveFramePointer -Xcomp
JAVAEXTFLAGS=-XX:+ExtendedDTraceProbes

build:
		mkdir -p bin
		cp mysql-connector-java-5.1.40-bin.jar bin/
		javac *.java -d bin

clean:
		rm -rf bin

allocy: build
		java $(JAVAFLAGS) -cp bin Allocy

allocyext: build
		java $(JAVAFLAGS) $(JAVAEXTFLAGS) -cp bin Allocy

clientygood: build
		sudo pip install dnslib
		sudo ./proxy.py > /dev/null &
		java $(JAVAFLAGS) -cp bin Clienty good

clientybad: build
		sudo pip install dnslib
		sudo ./proxy.py > /dev/null &
		java $(JAVAFLAGS) -cp bin Clienty bad

collecty: build
		java $(JAVAFLAGS) $(JAVAEXTFLAGS) -cp bin Collecty

computey: build
		java $(JAVAFLAGS) -cp bin Computey 2 4000000 4

computeyext: build
		java $(JAVAFLAGS) $(JAVAEXTFLAGS) -cp bin Computey 2 4000000 4

databasey: build
		java $(JAVAFLAGS) -cp "bin:bin/mysql-connector-java-5.1.40-bin.jar" \
						  Databasey
databaseyext: build
		java $(JAVAFLAGS) $(JAVAEXTFLAGS) \
			-cp "bin:bin/mysql-connector-java-5.1.40-bin.jar" Databasey

servery: build
		java $(JAVAFLAGS) -cp bin Servery

writey: build
		java $(JAVAFLAGS) -cp bin Writey

all: build
Loading