2024年9月多线程编程题目(java多线程编程)

 更新时间:2024-09-21 09:45:47

  ⑴多线程编程题目(java多线程编程

  ⑵楼主出现问题有点:.wake()这个方法也要同步啊,加关键字synchronized;.第一次调用st(方法后,available应该保持不变,这样才能保证线程b也打印..start,所以加个if判断一下就ok啦。publilassABC{booleanavailable=false;Stringname;intcount=;publicABC(Stringname){this.name=name;}publicsynchronizedvoidst(){System.out.println(“...start.“);count++;if(count》){available=true;}try{wait();}catch(Exceptione){}System.out.println(“...end.“);}publicsynchronizedvoidend(){System.out.println(“...end.“);}publicsynchronizedvoidwake(){notifyAll();}publicstaticvoidmain(Stringargs){//TODOAuto-generatedmethodstubABCx=newABC(“haha“);TestThreada=newTestThread(x);TestThreadb=newTestThread(x);=newC(x);(newThread(a)).start();(newThread(b)).start();(newThread(c)).start();}}classTestThreadimplementsRunnable{ABCabc;publicTestThread(ABCabc){this.abc=abc;}publicvoidrun(){if(!abc.available){abc.st();}elseabc.end();}}classCimplementsRunnable{ABCabc;publi(ABCabc){this.abc=abc;}publicvoidrun(){try{Thread.sleep();//System.out.println(“sjlfsjjfsdlj“);//验证是否执行到这里}catch(InterruptedExceptione){};abc.wake();}}

  ⑶Linux多线程编程

  ⑷编译时要用到pthread库:g-lpthread错误码位置:/usr/include/asm-generic/errno.hgpthread_create.c-lpthread思考:主子线程交替打印奇数偶数。思考:证明线程可以自己取消自己。思考:证明SIGKILL和SIGSTOP是无法阻塞的。/usr/include/bits/pthreadtypes.h中查看pthread_mutex_t思考:用多线程将一个文件.c拷贝个副本,.c,.c,.c思考:多个生产者和消费者思考:将互斥量等初始化使用pthread_once实现。思考:设置线程的分离属性,然后在新县城中获取自己的分离属性。

  ⑸java多线程编程题跪求求解

  ⑹package?.zhidao;public?class?Main?{????private?static?int?count=;????public?static?void?main(String?args)?{????????//?TODO?Auto-generated?method?stub??????for(int?i=;i《;i++)??????{??????????String?name?=?“人员“+(i+);??????????ManThread?manThread?=?new?ManThread(name);??????????manThread.start();??????}????}????????public?static?synchronized?void?go(String?name)????{????????count++;????????System.out.println(“线程“+Thread.currentThread().getName()+“执行,“+name+“正在通过山洞“);????????try?{????????if(count》=)????????{????????????System.out.println(“全员通过程序结束“);????????????????????}????????????Thread.sleep();????????}?catch?(InterruptedException?e)?{????????????//?TODO?Auto-generated?catch?block????????????e.printStackTrace();????????}????}}

  ⑺package?.zhidao;public?class?ManThread?extends?Thread?{????private?String?name;????public?ManThread(String?name)????{????????this.name?=?name;????}????public?void?run()?{????????Main.go(name);????}}

  ⑻Linux多线程编程的一道题

  ⑼次计算就算分成多线程,也看不出有多大的差别的!我用次计算验证一个主线程和个线程的耗时区别,结果时间还是差不多!现在的计算机太强大了,以你PC的主频.G来说,处理你这样的问题顶多一两秒。对于单核的CPU,多线程实际上还是在同一时间只做一件事,不过是CPU的利用率提高了而已。对于短时间的计算处理,不建议采用多线程,时间片在跳来跳去时候的现场保护和还原反而没有起到节省时间的效果。

  ⑽【在线】高分求助JAVA多线程编程题(极为简单的多线程显示字符

  ⑾publilassDemo{publicstaticvoidmain(Stringargs){newDemo().start();}publicvoidstart(){ThreadA=newThread(newRunnable(){publicvoidrun(){while(true){System.out.println(“A“);try{Thread.sleep();}catch(InterruptedExceptione){}}}});ThreadB=newThread(newRunnable(){publicvoidrun(){while(true){System.out.println(“B“);try{Thread.sleep();}catch(InterruptedExceptione){}}}});A.start();B.start();}}//已经编译运行过了over^_^

  ⑿JAVA多线程编程题两个案例,不会写,求大神写出代码,万分感谢,多线程还没学

  ⒀/*classAextendsThread{publicvoidrun(){try{Thread.sleep();}catch(Exceptione){System.out.println(“AERROR!“);}System.out.println(“AAAA“);}}classBextendsThread{publicvoidrun(){try{Thread.sleep();}catch(Exceptione){System.out.println(“BERROR!“);}System.out.println(“BBBB“);}}classCextendsThread{publicvoidrun(){try{Thread.sleep();}catch(Exceptione){System.out.println(“CERROR!“);}System.out.println(““);}}publilassTest_{publicstaticvoidmain(Stringargs){Aa=newA();Bb=newB();=newC();a.start();b.start();c.start();}}*/publilassTest_{publicstaticvoidmain(Stringargs){Aa=newA();Bb=newB();=newC();Threadta=newThread(a);Threadtb=newThread(b);Threadtc=newThread(c);ta.start();tb.start();tc.start();}}classAimplementsRunnable{publicvoidrun(){try{Thread.sleep();}catch(Exceptione){System.out.println(“AERROR!“);}System.out.println(“AAAA“);}}classBimplementsRunnable{publicvoidrun(){try{Thread.sleep();}catch(Exceptione){System.out.println(“BERROR!“);}System.out.println(“BBBB“);}}classCimplementsRunnable{publicvoidrun(){try{Thread.sleep();}catch(Exceptione){System.out.println(“CERROR!“);}System.out.println(““);}}案例一的两种方法已经写好;现在有事,晚上再把案例二代码写一下,应该没关系吧!抱歉,是一个线程类,我看错了,晚上重发一下代码!

  ⒁java多线程编程题之连续打印abc的几种解法

  ⒂package.demo.test;/***基于两个lock实现连续打印abcabc....*authorlixiaoxi**/publilassTwoLockPrinterimplementsRunnable{//打印次数privatestaticfinalintPRINT_COUNT=;//前一个线程的打印锁privatefinalObjectfontLock;//本线程的打印锁privatefinalObjectthisLock;//打印字符privatefinalcharprintChar;publicTwoLockPrinter(ObjectfontLock,ObjectthisLock,charprintChar){this.fontLock=fontLock;this.thisLock=thisLock;this.printChar=printChar;}Overridepublicvoidrun(){//连续打印PRINT_COUNT次for(inti=;i《PRINT_COUNT;i++){//获取前一个线程的打印锁synchronized(fontLock){//获取本线程的打印锁synchronized(thisLock){//打印字符System.out.print(printChar);//通过本线程的打印锁唤醒后面的线程//notify和notifyall均可,因为同一时刻只有一个线程在等待thisLock.notify();}//不是最后一次则通过fontLock等待被唤醒//必须要加判断,不然虽然能够打印次,但次后就会直接死锁if(i《PRINT_COUNT-){try{//通过fontLock等待被唤醒fontLock.wait();}catch(InterruptedExceptione){e.printStackTrace();}}}}}publicstaticvoidmain(Stringargs)throwsInterruptedException{//打印A线程的锁ObjectlockA=newObject();//打印B线程的锁ObjectlockB=newObject();//打印C线程的锁ObjectlockC=newObject();//打印a的线程ThreadthreadA=newThread(newTwoLockPrinter(lockC,lockA,’A’));//打印b的线程ThreadthreadB=newThread(newTwoLockPrinter(lockA,lockB,’B’));//打印c的线程ThreadthreadC=newThread(newTwoLockPrinter(lockB,lockC,’C’));//依次开启abc线程threadA.start();Thread.sleep();//确保按顺序A、B、C执行threadB.start();Thread.sleep();threadC.start();Thread.sleep();}}

  ⒃java编程题目:实现多线程累加,求代码

  ⒄照着下面的例子改一下:/***实现Runnable接口的类**authorleizhimin--::*/publilassDoSomethingimplementsRunnable{privateStringname;publicDoSomething(Stringname){this.name=name;}publicvoidrun(){for(inti=;i《;i++){for(longk=;k《;k++);System.out.println(name+“:“+i);}}}/***测试Runnable类实现的多线程程序**authorleizhimin--::*/publilassTestRunnable{publicstaticvoidmain(Stringargs){DoSomethingds=newDoSomething(“阿三“);DoSomethingds=newDoSomething(“李四“);Threadt=newThread(ds);Threadt=newThread(ds);t.start();t.start();}}执行结果:李四:阿三:李四:阿三:李四:李四:阿三:李四:阿三:阿三:Processfinishedwithexitcode、扩展Thread类实现的多线程例子/***测试扩展Thread类实现的多线程程序**authorleizhimin--::*/publilassTestThreadextendsThread{publicTestThread(Stringname){super(name);}publicvoidrun(){for(inti=;i《;i++){for(longk=;k《;k++);System.out.println(this.getName()+“:“+i);}}publicstaticvoidmain(Stringargs){Threadt=newTestThread(“阿三“);Threadt=newTestThread(“李四“);t.start();t.start();}}执行结果:阿三:李四:阿三:李四:阿三:李四:阿三:阿三:李四:李四:Processfinishedwithexitcode

  ⒅java题目编程题目多线程

  ⒆publilassDoubleThread{publicstaticvoidmain(Stringargs){Threadt=newThread(){Overridepublicvoidrun(){for(chari=’a’;i《=’z’;i++){System.out.println(i);}}};Threadt=newThread(){Overridepublicvoidrun(){for(chari=’A’;i《=’Z’;i++){System.out.println(i);}}};t.start();t.start();}}

您可能感兴趣的文章:

相关文章