alanch_522 alanch_522
关注数: 0 粉丝数: 6 发帖数: 88 关注贴吧数: 2
英国留学时的作业:2个多线程编程,不大明白,帮忙一下 A1: Multiple Threads Write a Java program which includes ‘public void static main’ and a class named TClass. TClass must be active – i.e. instances must each have their own thread of control. TClass has a single instance variable threadId: private int threadId; The value of threadId is set by TClass’s constructor: public TClass(int val) {threadId = val;} TClass’s thread should print the following message 10 times on the console (monitor): “Hello, I am thread xx” where xx is the value of instance variable threaded provided by the constructor. The thread should then terminate. ‘public void static main’ should create 5 instances of TClass, with each instance being initialised with a different (threadId) value between 0 and 4 inclusive. After ‘public void static main’ has started the 5 threads, it should wait for them to complete before printing the message: “All threads terminated” after which it should terminate itself.A2: Multiple Threads and Shared Objects In this second part of the assignment you are required to write a simple Java program that creates multiple threads which interact via a shared object. You will need to be able to do this in order to tackle subsequent parts of the assignment. Make a copy of the program from Assignment 1.1 and modify it as follows. A class called Buffer should be added to the program, which provides a simulation of a FIFO queue. class Buffer { private int count; private boolean empty, full; public Buffer() { count = 0; empty = true; full = false; } public void put(int ti) { … } public void get(int ti) { … } } Operation of put: If count is less than 10, put should increment it. If count is equal to 10 full is set to true and the calling thread should be suspended. put should print a message indicating the id of the calling thread, and whether or not the thread is to be suspended. empty should be set to false. Operation of get: If count is greater than 0 it should be decremented. If count is equal to 0 empty is set to true the calling thread should be suspended. get should print a message indicating the id of the calling thread, and whether or not the thread is to be suspended. full should be set to false. Hints: (i) The method declarations given above are incomplete and must be changed. (ii) The descriptions of put and get are incomplete and you will need to add code to ensure that the program does not deadlock (lock-up or freeze). You will need to modify TClass from assignment 1.1, and produce two classes TClassP and TClassC, both of which should be active. Both classes have a second instance variable beside threadId, namely an object of class Buffer and which is initialised in the constructor: private int threadId; private Buffer b; The value of threadId is set by TClass’s constructor: public TClass(int val, Buffer buf) { threadId = val; b = buf; } In both cases, you should modify the run method by changing the maximum number of loop iterations from 10 to 100 and by removing the println statement. In TClassP, the println statement should be replaced by a call to b.put, with the id of the thread as a parameter of the call: b.put(threadId); The thread should then sleep for a period GLOBAL.CONSTANTS.PSLEEP. In TClassC, the println statement should be replaced by a call to b.get, with the id of the thread as a parameter of the call: b.get(threadId); The thread should then sleep for a period GLOBAL.CONSTANTS.CSLEEP. public class GLOBAL_CONSTANTS { public static final int GLOBAL.CONSTANTS.PSLEEP = 1000; public static final int GLOBAL.CONSTANTS.CSLEEP = 2000; } Finally, modify ‘public void static main’ to create 3 objects of class TClassP, 2 of class TClassC and 1 of class Buffer. It should initiate the execution of the threads, then wait for the threads to terminate, print the same message as in Assignment 1.1, and then terminate.
1 下一页