28HW4Threads (1)

28HW4Threads (1) - CS108 Stanford Winter 2012 Handout#28...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS108, Stanford Handout #28 Winter 2012 Young HW4 Threads Original Problem Concepts and Some Sections by Nick Parlante Homework 4 gives us an opportunity to practice using Java Threads. The first programs also give you a chance to run programs from the command prompt. This assignment is due midnight of the evening of Wednesday, February 15th. Reminder: This is the last assignment you may use late days for. Note: Most of the problems in this assignment have no starter java files. With the exception of the Cracker and WebWorker problems, you’ll need to create your classes from scratch. For those who haven’t run java from the command prompt yet, here is a brief overview. To run a Java program from the command prompt, bring up a console window on your computer. Change directory to where the Java bytecode for your Java files is located. 1 Run the public static void main method for one of your classes by typing java followed by the name of the class followed by any arguments you want to pass to the main method. For example: > java Bank small.txt 1 will execute the public static void main method from the class named Bank . The arguments "small.txt" and "1" will be passed to the main method and can be accessed through the args parameter as args[0] and args[1] . All arguments received will be strings. If you want to convert them to integers, you can use something like the Integer class’s parseInt method. Eclipse can simulate running from the command prompt. If you right-mouse on a class’s name go to the “Run As” menu and choose “Run Configurations”. Switch to the second tab in the dialog box which comes up and type whatever arguments you want to pass to your program. Now on to this assignment’s problems: Bank Multiple threads are a natural fit for programs involving producing and consuming items. One set of threads can be assigned to producing items while a separate set of threads can handle consumption. Solving producer/consumer type problems with multiple threads leads to an efficient use of resources. Multi-threading works particularly well if producing threads and consuming threads require different resources—for example if producing items is disk- intensive, whereas consuming items is CPU-bound. 1 Java source files (*.java) compile into Java bytecode (*.class). If you create and edit your files in Eclipse, the *.java files should be in a subcirectory labeled src and the *.class files will be in a separate subdirectory called bin.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Problem For this problem we will simulate a bank. Our bank will track the balances in twenty different accounts. When the program begins, each of the accounts contains $1000. The program will process a list of transactions which transfer money between the accounts. Once all transactions have been processed the program will go through and for each account it will print both the account’s final balance and the number of transactions (deposits and withdrawals) which occurred on that account. Our bank program will use the main thread to read the list of banking transactions from a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 17

28HW4Threads (1) - CS108 Stanford Winter 2012 Handout#28...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online