Вы находитесь на странице: 1из 5

CSCI 340

Lecturer: Simina Fluture


Project 1: DUE July, 25th (Saturday) 5PM

Any questions related to this project should be emailed to the Teaching


Assistant at jlaiqc@gmail.com (with a cc to me). The TA will be the one that
will grade your projects and respond to most of your questions.
Through its implementation, this project will familiarize you with the creation and
execution of threads, and with the use of the Thread class methods. In order to
synchronize the threads you will have to use (when necessary), synchronized
methods, join( ), yield( ), sleep(time), isAlive( ). In synchronizing threads, do not
use any semaphores, wait(), notify() or notifyAll() methods. You can use the
synchronized keyword at the level of methods but not for blocks of code.

The Scarecrow-Robot Factory


Simulate a scarecrow-robot factory using Java-threads. A Robot
Factory assembles robot parts that are supplied by partmanufacturers. The possible parts are legs, torsos, hands, heads, and
brooms. A robot is completed when it has 2 legs, 2 hands, 1 torso, 1
head and 1broom. Each manufacturer produces only one type of
parts. That means if it produces heads then it can't produce anything
else;
Once a part is produced, the manufacturer uses a truck to deliver that
part to the factory. Use a random time to simulate production,
ranging from PRODUCTION_TIME_MIN to PRODUCTION_TIME_MAX. A
truck is a short-lived thread that is created by the manufacturer only
for the purpose of delivering a part. On its way to delivery, the truck
speeds up. Simulate it by increasing the truck priority using the
getPriority()and setPriority() methods. When the truck arrives to
the factory, its priority value is set back to default. The truck thread
delivers the part to the factory, and once the part is accepted, the
truck goes back to its manufacturer and let it know if production
should be halted momentarily. Production should be halted if there is
a surplus of certain type of parts. The temporary halt can be
simulated using sleep(random-time). It's up to the factory to
determine what part type is surplus by doing the following
computation:
maxType = maximum(all available parts count by type);

(in the case of a tie, take first found)


maxUsedUp = count how many maxType can be used to make max
possible number of complete scarecrow
isSurplus = maxType - maxUsedUp > 3

Note: Units that are used in pairs count as 1, meaning that 2 hands =
1 head when counting surplus.
When a truck delivers parts it gets in line with other trucks.
Note: The line can be simulated using java.util.Vector. Since Vector is
already synchronized, we do not need to synchronize when
removing/inserting elements.
When the first truck in line moves to factory, it is told if its part is
considered to be surplus. If it is surplus, the truck decides to go at the
end of the line and retry. It also allows some incoming new trucks to
get on the line first (use yield ()). If a truck is on its second attempt
to get its part accepted by the factory, it is accepted, even if it is still
surplus.
When a manufacturer gets halted, it halts for time HALT_TIME (the
default is equal to PRODUCTION_TIME_MAX). Any other truck that
comes back from the factory and announces that manufacturer's part
is still surplus doesn't have any effect while manufacturer is halted.
The factory runs for time OPERATION_TIME. When the time left for
the operation equals OPERATION_TIME/5, each manufacturer is
announced about it in sequence. The order of sequence is from the
manufacturer that needs the parts the most, to the one that needs the
parts the least. Use join() to achieve proper sequence; isAlive() is
not really needed here since the factory will tell what to join, and
which manufacturer threads are still alive at that time. However will
be interested to use it in your implementation.
Note:
As an example, assume manufacturer M is needed most. It doesn't
join any other thread and continues to produce parts. Manufacturer
M1 that is needed less than M, joins M and, so on.
( M2 joins M1,..., MN joins M(N-1)).

SUGGESTIONS FOR YOUR IMPLEMENTATION


Some synchronized (for implementing Mutual Exclusion) methods that you need to use:

Factory:
public synchronized boolean acceptPart( Truck truck ) {
/*
* write code to see if truck carries part that is surplus. Skip
* this part if truck is retrying - just accept.
*
* write code to update factory inventory of parts
*/
}

Manufacturer:
public synchronized void setIsSurplus( boolean isSurplus ) {
// write code
}
public synchronized boolean isSurplus() {
// write code
}
public synchronized void factoryIsAboutToStopOperation
( Manufacturer manufacturerToJoin ) {
// write code
}
public synchronized boolean isFactoryAboutToStopOperation() {
// write code
}

Guidelines
1. Do not submit any code that does not compile and run. If there are parts
of the code that contain bugs, comment it out and leave the code in. A
program that does not compile nor run will not be graded.
2. Follow all the requirements of the Projects description.
3. Main class is run by the main thread. The other threads must be manually
specified by either implementing the Runnable interface or extending the
Thread class. Separate the classes into separate files. Do not leave all the
classes in one file. Create a class for each type of thread.
4. The program asks you to create two different types of thread. A type can
have more than one instance of the thread.

No manual specification of each thread's activity is allowed (e.g. no


Thread1.doErrands()).
5. Add the following lines to all the threads you make:
public static long time = System.currentTimeMillis();
public void msg(String m) {
System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);
}
6. There should be printout messages indicating the execution interleaving.
Whenever you want to print something from that thread use: msg("some
message here");
7. NAME YOUR THREADS or the above lines that were added would mean
nothing.
Here's how the constructors could look like (you may use any variant of this
as long as each thread is unique and distinguishable):
// Default constructor
public RandomThread(int id) {
setName("RandomThread-" + id);
}
8. Design an OOP program. All thread-related tasks must be specified in its
respective classes, no class body should be empty.
9. No implementation of semaphores is allowed.
10. "Synchronized" is not FCFS implementation. Synchronized keyword in
Java allows a lock on the method or on an object; it is used to enforce
mutual exclusion on the critical section.
11. DO NOT USE System.exit(0); the threads are supposed to terminate
naturally by running to the end of their run methods.

12. Javadoc is not required. Proper basic commenting explaining the flow of
program, self-explanatory variable names, correct whitespace and
indentations are required.
Tips:
-If you run into some synchronization issue, and don't know which thread or
threads are causing it, press F11 which will run the program in debug mode.
You will clearly see the thread names in the debug perspective.

Setting up project/Submission:
In Eclipse:
Name your project as follows: LASTNAME_FIRSTNAME_CSXXX_PY
where LASTNAME is your last name, FIRSTNAME is your first name, XXX is
your course, and Y is the current project number.
For example: Fluture_Simina_CS340_p1
To submit:
-Right click on your project and click export.
-Click on General (expand it)
-Select Archive File
-Select your project (make sure that .classpath and .project are also
selected)
-Click Browse, select where you want to save it to and name it as
LASTNAME_FIRSTNAME_CSXXX_PY
-Select Save in zip format, Create directory structure for files and also
Compress the contents of the file should be checked.
-Press Finish
Email the archive with the specific heading:

CS340 Project # Submission: Last Name, First Name to jlaiqc@gmail.com


with a cc to simina.fluture@gmail.com
You should receive an acknowledgement within 24 hours.

Вам также может понравиться