程序代写案例-COSI 131A

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
1
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Concurrent Shell Tutorial
October 7, 2021
2
Brandeis Universi
ty - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Outline
● PA1-1
● Runnables and Threads
● LinkedBlockingQueue
● Poison Pill
● Handling Interrupts
● Background Commands
● Grading
● Additional Resources
3
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
PA1-1
4
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Submission must be an Eclipse project based on the skeleton.
○ You must import the skeleton into Eclipse using Eclipse’s Import feature.
○ You must export your project to a zip file using Eclipse’s Export feature.
● Failing to do so will result in a zero.
● Submission zip file and project should be named with your lastname
and firstname.
○ You must do this using Eclipse’s Refactor > Rename feature.
e.g. submission file: ChamiLamelasPA1-1.zip
Eclipse project: ChamiLamelasPA1-1
Submission
5
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● AllConcurrentTests.java throwing an Exception likely means
you haven’t properly opened and closed your Java reader and writer
objects.
○ Scanner
○ PrintWriter, PrintStream
Test Problems
6
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● TAs cannot grant extensions, only Professor Shrira.
● If you are granted an extension, you must submit your work by sharing it
on Google drive with Eitan and Chami or it will not be accepted.
[email protected]
[email protected]
● If you do not do well on one PA, you will not fail the course.
○ Future PAs conceptually more difficult.
Extensions and Grade
7
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Runnables and Threads
8
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Any class you wish to run in a thread must implement this interface.
● Thread behavior is specified by overriding the run()method.
public class PositivePrinter implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
}
}
Runnable
9
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Runnable classes are executed as Thread objects.
● To turn a Runnable object into a Thread, pass it into the Thread
constructor.
● To create a thread in the JVM and execute the Runnable in the thread, call
the start() method.
PositivePrinter positivePrinter = new PositivePrinter();
Thread thread = new Thread(positivePrinter);
thread.start();
Code with no outer block is in main().
Making and Starting a Thread
10
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Consider this code:
PositivePrinter positivePrinter = new PositivePrinter();
Thread thread = new Thread(positivePrinter);
thread.start();
System.out.print("Main is done ");
● No guarantee "Main is done " will print after positivePrinter is
done printing, could see: Main is done 1 2 3 4 5 6 7 8 9 10
Thread Execution Order
11
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Use join() to ensure positivePrinter finishes printing before main()
prints.
● join() halts execution of current Thread until the invoking Thread dies.
PositivePrinter positivePrinter = new PositivePrinter();
Thread thread = new Thread(positivePrinter);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {}
System.out.println("Main is done ");
Joining a Thread
12
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Create a class NegativePrinter that can be executed as a Thread
that prints the numbers -1 to -10 on the same line.
public class NegativePrinter implements Runnable {
@Override
public void run() {
for (int i = 1; i < 10; i++) {
System.out.print((-i) + " ");
}
}
}
Exercise: Making a Runnable
13
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Create a class NegativePrinter that can be executed as a Thread
that prints the numbers -1 to -10 on the same line.
public class NegativePrinter implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.print((-i) + " ");
}
}
}
Exercise: Making a Runnable (Solution)
14
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Is this the correct way to create and execute a NegativePrinter as a
thread?
NegativePrinter negativePrinter = new NegativePrinter();
Thread thread = new Thread(negativePrinter);
thread.run();
Exercise: Executing a Runnable
15
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Is this the correct way to create and execute a NegativePrinter as a
thread?
NegativePrinter negativePrinter = new NegativePrinter();
Thread thread = new Thread(negativePrinter);
thread.run();
● No. You need to use thread.start().
Exercise: Executing a Runnable (Solution)
16
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Is this the correct way to create and execute a NegativePrinter as a
thread?
NegativePrinter negativePrinter = new NegativePrinter();
Thread thread = new Thread(negativePrinter);
thread.run();
● No. You need to use thread.start().
At home: thread.run() will compile because it is a method in Thread. Read
the Thread documentation to figure out why you shouldn’t use it.
Exercise: Executing a Runnable (At Home)
17
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
LinkedBlockingQueue
18
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Queue implemented using a linked list that is thread-safe and will block
(wait) on addition and removal operations if necessary.
● put(E e) will try to place e at the end, waiting until there’s space.
● take() will try to remove (and return) the first element, waiting until the
queue is non-empty.
● new LinkedBlockingQueue() will create a queue with capacity
Integer.MAX_VALUE.
LinkedBlockingQueue
19
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Where in the PA may you need to wait to retrieve the head of a queue?
When a subcommand has finished its processing and is waiting for the
previous subcommand to give it more input to process.
● Where may you need to wait to add to the end of a queue?
When a subcommand has finished its processing but the next
subcommand is still processing its input and has not passed it further
down the pipeline.
Need for Blocking
20
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Where in the PA may you need to wait to retrieve the head of a queue?
When a subcommand has finished its processing and is waiting for the
previous subcommand to give it more input to process.
● Where may you need to wait to add to the end of a queue?
When a subcommand has finished its processing but the next
subcommand is still processing its input and has not passed it further
down the pipeline.
Need for Blocking (Answer)
21
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Poison Pill
22
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: The Producer-Consumer Paradigm is a programming pattern
where producers create and deliver messages that consumers read and
handle.
Definition: A poison pill is a predefined data item which producer processes
can use to communicate to consumer processes to shut down.
Poison Pill
Producer ConsumerMessage3 Message2 Message1 Message0
Running Running
23
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: A buffer is a temporary data storage mechanism used for
transferring data between processes.
Property: A producer process will write messages to a buffer. When the
producer wants to terminate a consumer, it adds a poison pill to the shared
buffer.
Poison Pill - Producer
Producer Poison_Pill Message3 Message2 Message1 Message0
Running
24
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Property: A consumer process that knows to look for a specific poison pill will
terminate early when read.
Poison Pill - Consumer
ConsumerPoison_Pill
Message1
Message2
Message3
Message0
Processed Messages
Halted
25
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● With poison pill, a Filter communicates to its next Filter when it is
finished.
● A Filter can also mark itself as finished with a flag.
At home: how will a Filter now know if the previous Filter is finished?
Alternative Approach
26
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Handling Interrupts
27
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: An interrupt in Java is an indication to a thread that it should stop
its current behavior and do something else.
Interrupts
28
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: An interrupt in Java is an indication to a thread that it should stop
its current behavior and do something else.
Exercise: Suppose that you were implementing the cat command to run in a
thread. What would you have to do when handling an interrupt?
Interrupts - Exercise
29
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: An interrupt in Java is an indication to a thread that it should stop
its current behavior and do something else.
Exercise: Suppose that you were implementing the cat command to run in a
thread. What would you have to do when handling an interrupt?
Close any Java file reading objects (Scanner).
Interrupts - Exercise (Solution)
30
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: An interrupt in Java is an indication to a thread that it should stop
its current behavior and do something else.
Exercise: Suppose that you were implementing the cat command to run in a
thread. What would you have to do when handling an interrupt?
Close any Java file reading objects (Scanner).
At home: Which other filters would require similar clean up when handling
interrupts?
Interrupts - Exercise (At Home)
31
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
A thread thread1 sends an interrupt to thread thread2 by calling
thread2.interrupt()
Note: This requires thread1 to have access to the reference to thread2
Example:
public static void main(String[] args){
Thread thread2 = new Thread(new PositivePrinter());
thread2.start()
thread2.interrupt();
}
Sending an Interrupt In Java
32
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
A thread thread1 sends an interrupt to thread thread2 by calling
thread2.interrupt()
Note: This requires thread1 to have access to the reference to thread2
Example:
public static void main(String[] args){
Thread thread2 = new Thread(new PositivePrinter());
thread2.start()
thread2.interrupt();
}
Sending an Interrupt In Java
In this example, thread1
is the main method
33
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
A Thread must support its own interrupt handling.
Two basic ways to handle interrupts in Java in a Runnable class:
1. Catching InterruptedException
- Some methods throw this if the current Thread has been
interrupted.
2. Checking Thread.currentThread.isInterrupted()
- isInterrupted() tells you whether the invoking Thread has
been interrupted.
Handling Interrupts in Java
34
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
PositivePrinter needs to be edited so that thread2 will handle the
interrupt. Can catch InterruptedException:
public class PositivePrinter implements Runnable {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
Thread.sleep()
System.out.print(i + " ");
}
} catch (InterruptedException e) { // handle interrupt here… }
}
}
Handling Interrupts - InterruptedException
35
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
PositivePrinter needs to be edited so that thread2 will handle the
interrupt. Can catch InterruptedException:
public class PositivePrinter implements Runnable {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
Thread.sleep()
System.out.print(i + " ");
}
} catch (InterruptedException e) { // handle interrupt here… }
}
}
Handling Interrupts - InterruptedException
Thread.sleep() is an example of a method
that throws InterruptedException when
interrupted.
36
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
You must utilize methods that throw InterruptedException to use the
InterruptedException catching method.
Useful methods that throw InterruptedException:
● LinkedBlockingQueue.take()
● LinkedBlockingQueue.put(E e)
● Thread.sleep(): causes the currently executing Thread to cease
execution.
Methods That Throw InterruptedException
37
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
PositivePrinter needs to be edited so that thread2 will handle the
interrupt. Can check isInterrupted():
public class PositivePrinter implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
if (Thread.currentThread.isInterrupted()){
// handle interrupt here...
}
System.out.print(i + " ");
}
}
}
Handling Interrupts - isInterrupted()
Note that this static call will not
clear the internal interrupted flag.
38
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Background Commands
39
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Definition: A background process is a process that runs concurrently to the
user facing processes.
Background processes are generally used to perform computations
independent of a control process, freeing the control process to complete
other tasks.
In PA1-2 you need to support the ability for shell commands to be run as
background processes if the user follows a command with &.
Background Commands
40
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
In PA1-2, the REPL is the user facing process.
Example:
Pipelines as Background Commands
cmd1 cmd3Foreground:
Background: cmd2 &
cmd4
cmd5 &
cmd6 &
cmd7
41
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
In PA1-2, the REPL is the user facing process.
Example:
Pipelines as Background Commands
cmd1 cmd3Foreground:
Background: cmd2 &
cmd4
cmd5 &
cmd6 &
cmd7
Background commands must be started
between foreground commands
42
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
In PA1-2, the REPL is the user facing process.
Example:
Pipelines as Background Commands
cmd1 cmd3Foreground:
Background: cmd2 &
cmd4
cmd5 &
cmd6 &
cmd7
Background commands must be started
between foreground commands Foreground commands can only be started
separately from other foreground commands, but
can be started concurrently to background
commands
43
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
In PA1-2, the REPL is the user facing process.
Example:
Pipelines as Background Commands
cmd1 cmd3Foreground:
Background: cmd2 &
cmd4
cmd5 &
cmd6 &
cmd7
Background commands must be started
between foreground commands Foreground commands can only be started
separately from other foreground commands, but
can be started concurrently to background
commands
Background commands can be started
concurrently.
44
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Commands that have been killed should have their associated Threads
properly interrupted.
● Interrupted Threads should cease execution of their assigned task (i.e.
whatever command they are).
Definition: A Thread is dead if it has completed execution of its underlying
Runnable’s run() method.
● For repl_jobs: check if a background command (job) is still running
by checking if it’s not dead using Thread’s isAlive() method.
Viewing and Killing Background Jobs
45
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Grading
46
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Same 60 test cases from PA1-1 + 4 background command tests.
● Important: Passing test cases from PA1-1 does not mean you have a
correct concurrent solution.
○ Run the (sequential) starter kit and you pass 60/64.
○ All the (foreground) commands from PA1-1 must pass the tests when
implemented as Threads that can run concurrently.
● Run AllConcurrentTests.java 10 times to check for race
conditions (errors dependent on sequence of execution).
Tests
47
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
Additional Resources
48
Brandeis University - COSI 131A Fall 2021 - Eitan Joseph and Chami Lamelas
● Runnable
● Thread
● LinkedBlockingQueue
Java Documentation

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468