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.
我该追她吗? 我在英国留学,但是国内的时候一直喜欢一个女孩子,有2年了.她在国内读大学...她不知道我喜欢她,我们关系也很普通.我一直没敢接近她,因为我们相隔的太远了,怕她受伤.所以我一直把这份感情藏在心里,想她就偷偷的去她的空间看一眼... 所以2年前我就下决心把这感情藏起来,打算读研毕业回来追她...但是这种相思太煎熬了,日日夜夜想她,睡不着...我快受不了了...我应该告诉她吗?我怕等我回国就没有机会了... 兄弟们,救命啊...
吧里的"坏蛋"兄弟们给我出个主意~~ 我在英国留学,但是国内的时候一直喜欢一个女孩子,有2年了.她在国内读大学...她不知道我喜欢她,我们关系也很普通.我一直没敢接近她,因为我们相隔的太远了,怕她受伤.所以我一直把这份感情藏在心里,想她就偷偷的去她的空间看一眼...所以2年前我就下决心把这感情藏起来,打算读研毕业回来追她...但是这种相思太煎熬了,日日夜夜想她,睡不着...我快受不了了...我应该告诉她吗?我怕等我回国就没有机会了...兄弟们,救命啊...
等302的兄弟都顶起来~ 道哥因为读者顶起昨天连夜多更新了一章,今天难道就要少更新一章?
302还没出呢? RT
我晕,成日本动画片了,收拾个小搂搂都几章了,太吊胃口了,道哥~~~ 还要几章啊...情节发展太慢了
要你命8767816 你真要自己命了!(吃饱撑死的)
最近大家都在听点什么好听的歌,推荐下~ 最近大家都在听点什么好听的歌,推荐下~
原来道哥肺炎了啊~
道哥哥哥哥哥~~~,你在哪里啊!?我想看坏蛋蛋蛋蛋蛋~~~
怎么感觉现在一天只有一章了? 如题,还是我的错觉?总觉得少了那么几章
怎么突然一个吧主都没了? 如题,很费解
你们尽吧主的责任了吗?担子越大,责任越大,这个道理都不懂 现在的3个吧主 我超级鄙视你们[ 资深潜水员] 自重换吧主以来 你们一直就不能像原来一样的吧主尽心尽责 至于例子 我不多说 就拿 挖坟来说 如果是以前的吧主 黑色 他们绝对会以最快时间清除那些垃圾 而你们呢 只是等到大家都 强调N遍才出来解决问题 而且 还动不动就失踪 我怀疑你们每天有几个小时能在线 就这种素质 还来当吧主 我什么话都不想说 只说 现在的坏蛋吧 让人感觉很 无奈 也很 让人伤心 如果你们没有那个能耐 请尽早让 原来的人回来
1
下一页