Linux 多线程 Advanced Level Tasks (5 marks plus 1 bonus makr):.

月亮痕2022-10-04 11:39:541条回答

Linux 多线程
Advanced Level Tasks (5 marks plus 1 bonus makr):.
For the following tasks,you should write a proper report to explain the logic of your programs and also answer the questions from the tasks.Your report should read like a textbook on programming.You can use figures and pieces of program code in the report to help you to explain.The full programs and the program outputs should be included at the end of the report as an appendix.A submission with only the programs and without the report will not attract any mark; neither will a report without the programs.
Task 1:Below is a program which mimics the web page counter,counting how many visits to a web page.The counter increases by 1 for every visit.To mimic multiple concurrent visits,a new thread is created in response to a connection from a remote web browser.A real web server actually behaves like this.The variable cnt is the counter and is shared by all threads.
#include
#include
#include
// repeat 100 times to mimic 100 random visits to the page
#define RPT 100
//web page visit counter
int cnt=0;
void* counter() {
int cntLocalCopy;
float r;
cntLocalCopy = cnt;
// mimicking the work of the sever in serving the page to
// the browser
r = rand() % 2000; usleep(r);
cnt = cntLocalCopy + 1;
}
int main () {
int i;
float r;
pthread_t tid[RPT];
// seed the random number sequence
srand(time(NULL));
for (i=0; i

已提交,审核后显示!提交回复

共1条回复
快乐薰衣草 共回答了16个问题 | 采纳率100%
pthread库也有mutex可以用来同步的,难道你没有用?如果真的没有用,那就不奇怪了.
1年前

相关推荐

用多线程编程实现两组数据 ({1.2.3.4.5}{A.B.C.D.E}),以下列顺序显示:5.E.
venry1年前1
vickimeimei 共回答了25个问题 | 采纳率92%
#include #include DWORD WINAPI Fun1Proc(LPVOID lpParameter); DWORD WINAPI Fun2Proc(LPVOID lpParameter); int a[5]={1,2,3,4,5}; char b[6]={'A','B','C','D','E'}; void main() { HANDLE hThread1; HANDLE hThread2; hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL); hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL); CloseHandle(hThread1); CloseHandle(hThread2); Sleep(15); } DWORD WINAPI Fun1Proc(LPVOID lpParameter) { cout
java面试题:请用多线程显示,每三个数字为一组,直到30.
java面试题:请用多线程显示,每三个数字为一组,直到30.
线程A:1
线程A:2
线程A:3
线程B:4
线程B:5
线程B:6
线程A:7
线程A:8
线程A:9
...(直到显示30)
wml001_11年前1
laike27 共回答了15个问题 | 采纳率93.3%
/>public class Test{
public static Object obj = new Object();
public static void main(String[] args){
new A().start();
new B().start();

}
}

class A extends Thread{
public void run(){
try{
synchronized(Test.obj){
for(int i = 1 ; i < 31;i += 6){
Test.obj.notify();
System.out.println("线程A:"+ i);
System.out.println("线程A:"+ (i+1));
System.out.println("线程A:"+ (i+2));
Test.obj.wait();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}

class B extends Thread{
public void run(){
try{
synchronized(Test.obj){
for(int i = 4 ; i < 31;i += 6){
Test.obj.notify();
System.out.println("线程B:"+ i);
System.out.println("线程B:"+ (i+1));
System.out.println("线程B:"+ (i+2));
Test.obj.wait();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
求教问题 JAVA 多核多线程的 该怎么写
求教问题 JAVA 多核多线程的 该怎么写
某公路桥很长但非常窄,宽度只能容纳一辆汽车通过,桥上不能错车,也不能调头。同一方向的汽车可以排成一队依次过桥。如果有车正在沿某一方向过桥,那么另一方向的车不允许上桥。编写线程模拟不同方向的汽车过桥的过程。(每辆汽车一个线程)
1203334589
风景驽钝1年前1
zz小雪 共回答了19个问题 | 采纳率84.2%
public class ThreadTest {

public static void main(String[] args) {

ThreadTest test = new ThreadTest();
for(int i=0; i<10; i++){
new MyThread("线程: " + i,test).start();
}
}


public synchronized void bridge(){
System.out.println(Thread.currentThread().getName() + "正在过桥");
try {
Thread.sleep(1000); //过桥需要1000毫秒
} catch (Exception e) {
// TODO: handle exception
}
}
}

class MyThread extends Thread{
private ThreadTest threadTest;

public MyThread(String name,ThreadTest threadTest) {
super(name);
this.threadTest = threadTest;
}

@Override
public void run() {
System.out.println(this.getName() + "准备过桥");
threadTest.bridge();
System.out.println(this.getName() + "已经过桥了");
}
}
Java多线程问题,我不大懂。创建两个Runnable,其中一个的run()方法启动并调用wait(),而第二个类应捕获
Java多线程问题,我不大懂。
创建两个Runnable,其中一个的run()方法启动并调用
wait(),而第二个类应捕获第一个Runnable对象的引用,其
run()方法应该在数秒后,为第一个任务调用notifyAll(),从而
使第一个任务可以显示一条信息。
发光物体1年前1
小刀帅哥 共回答了21个问题 | 采纳率95.2%
不是非常清楚你想做什么。但是我想你应该是想让第二人线程在第一个线程是wait()(满足一定条件),然后再继续执行(满足一定条件)是吧!
线程A:
方法a1(){一定条件。。。wait()}
a2(){一定条件。。。notify()}
线程B:
方法b1(){调用线程A的方法。。。a1()}
在调用a1的时候满足了条件,那么这个线程就会在那等待。等到A线程在调用方法a2的时候满足条件就会叫醒在些wait()的线程!