Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COM2007 Lab5  1 
Introduction to JCSP 
COM2007  
Lab 5 
 
Purpose 
Learning Outcome: You will understand how to put processes in parallel using a specialised 
Java library which supports concurrency. 
 
 
Note: In using this document it is possible to paste text directly into Eclipse. That means 
when defining new terms in Eclipse their value can be copied directly from here, which will 
remove the possibility of mistyping values.  
 
If you are reading a PDF version of the document, then clicking on the ‘Select’ icon activates 
text selection: 
 
 
 
Setting Up JCSP in Eclipse 
 
• You will need to download the .jar files. Go to: 
http://www.cs.kent.ac.uk/projects/ofa/jcsp/ 
• Download the latest release. Store a copy of this in your home directory. 
Unzip it so that you can see the following: 
 
 
 
COM2007 Lab5  2 
• Then load up Eclipse and import the .jar file from the lectures tab. You should be able 
to see: 
 
 
 
 
 
• Make sure the build path is set up properly: 
 
 
• You can also do the same on the command line but will need to make sure that your 
class path is set appropriately. 
 
Getting Started – JCSP 
Example 1 
• Try out some code which does not communicate between each other.  It is the code from 
lecture 8 and can be found in the lec8 package in the  com2007_wk5 project. It will contain 
three files: 
o ProcessOneLec8 
o ProcessTwoLec8 
COM2007 Lab5  3 
o RunMeLec8 
• You will see as output two counters being updated independently, so they are executing in 
parallel without knowing anything about each other. 
 
Example 2 
• Try out some sample code which communicates between objects. These files will be stored 
under the lec9 package: 
o PLec9.java 
o QLec9.java 
o ExampleLec9Network.java 
• Run the example. Does the program terminate? No, because both the run methods of the P and 
Q objects are running forever. Press stop in the Eclipse console. 
• Open up a web browser and go to http://www.cs.kent/projects/ofa/jcsp. Find the Browse the 
documentation online. Find the jcsp.lang package, then look under the class summary for 
details of the class One2OneChannelInt. This is the communication channel used in our first 
example. Familiarise yourself with the method summary so that you can see that the read and 
write methods are those that P and Q use in their respective definitions. 
 
 
Example 3 (new to try) 
• Import the com2005_lab5.zip in order to import the org.com2005.lab5 project In the Java 
project there is a package called lab_wk5. This contains two files: 
o Filter.java and  
o Lab5Example2.java. 
• Fill in the body of the Filter.java so that it does what the comment says. Then compile and run 
the program. 
• Notice that this time we make use of another built in class NumbersInt. This is an integer 
generator. It produces integers on its output channel. You will find it in the 
org.jcsp.plugNplay.ints. Familiarise yourself with the documentation of this class. 
 
Now try the exercises on the other sheets