线程编程题
⑴ C# winForm Socket 多线程 编程 关闭问题
2中方式,除主线程外其他线程设置为后台线程,即background thread,这是线程创建时的一个属性,另一种就是应用程序退出时的事件里手动退出线程
----
void Application_Exit(object sender,args e)
{
//thread.abort();
}
好像是这样写吧,好久没用C#了,有点忘了,MSDN上有详细的说明
socket是非托管资源,我的理解是最好首先清理(在其他线程前);
⑵ linux系统下,c语言pthread多线程编程传参问题
3个线程使用的都是同一个info
代码 Info_t *info= (Info_t *)malloc(sizeof(Info_t));只创建了一个info
pthread_create(&threads[i],NULL,calMatrix,(void *)info); 三个线程使用的是同一个
我把你的代码改了下:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
intmtc[3]={0};//resultmatrix
typedefstruct
{
intprank;
int*mta;
int*mtb;
}Info_t;
void*calMatrix(void*arg)
{
inti;
Info_t*info=(Info_t*)arg;
intprank=info->prank;
fprintf(stdout,"calMatrix:prankis%d ",prank);
for(i=0;i<3;i++)
mtc[prank]+=info->mta[i]*info->mtb[i];
returnNULL;
}
intmain(intargc,char**argv)
{
inti,j,k=0;
intmta[3][3];
intmtb[3]={1};
Info_t*info=(Info_t*)malloc(sizeof(Info_t)*3);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
mta[i][j]=k++;
/*3threads*/
pthread_t*threads=(pthread_t*)malloc(sizeof(pthread_t)*3);
fprintf(stdout," ");fflush(stdout);
for(i=0;i<3;i++)
{
info[i].prank=i;
info[i].mta=mta[i];
info[i].mtb=mtb;
pthread_create(&threads[i],NULL,calMatrix,(void*)(&info[i]));
}
for(i=0;i<3;i++)
pthread_join(threads[i],NULL);
fprintf(stdout," ====thematrixresult==== ");
fflush(stdout);
for(i=0;i<3;i++)
{
fprintf(stdout,"mtc[%d]=%d ",i,mtc[i]);
fflush(stdout);
}
return0;
}
矩阵的计算我忘记了,你运行看看结果对不对
⑶ 求解java编程题:编写一个应用程序,创建三个线程分别显示各自的运行时间
用Calendar的话,打印时间的地方就改团梁为Calendar.getInstance().getTime()
我很奇怪,为什源袜么雹或激代码贴不上来??图片也插入不了?
⑷ 璇剧▼璁捐¢樼洰锛屽氱嚎绋嬬紪绋嬶细鍖婚櫌闂ㄨ瘖妯℃嫙锛屾兂鐢╦ava瀹炵幇锛屾眰澶х炴寚镣
鍏稿瀷镄勭敓浜ц呮秷璐硅呮ā鍨嬨
浜呜Вj5镄勫苟鍙戝簱锛岄偅涓骞跺彂搴扑腑链夐傚悎缁勪欢瀹炵幇銆
濡傛灉涓崭简瑙o纴杩欎箞𨱒ワ细
鍒涘缓涓涓阒熷垪锛屾ら槦鍒楄佹眰绾跨▼瀹夊叏锛屽傛灉阒熷垪涓虹┖鍒欐秷璐硅呴樆濉炪傚傛灉阒熷垪杈惧埌镆愪釜链澶у硷纴鍒欓樆濉炵敓浜ц呫
阒熷垪鐢锛屾櫘阃氱殑list鎴栧疄鐜板ソ镄勯槦鍒楀寘瑁呮垚绾跨▼瀹夊叏镄勚
鐢╯ynchronized钖屾ュ师鏂规硶鎴栦唬镰佸潡銆
鍐欎竴涓鎴杗涓绾跨▼锛屾ā𨰾熺梾浜猴纴鎺挜槦锷炵悊涓氩姟锛屽线涓婇溃镄勯槦鍒椾腑娣诲姞鏁版嵁銆
褰撹揪鍒伴槦鍒楃殑链澶у圭Н锛岄樆濉烇纴绛夊緟鐢熶骇钥呯嚎绋嫔彇鏁版嵁銆
阒诲烇细makerLock.wait();//铏氭嫙链轰细鍑鸿╃嚎绋嬫寕璧凤纴鍏跺疄灏辨槸镎崭綔绯荤粺锛屼缭瀛桦綋鍓岖嚎绋嫔湪cpu涓婄殑杩愯岀姸镐併傚啀鍑鸿╃嚎绋嬫e湪浣跨敤镄刢pu璧勬簮锛屽崰鐢ㄧ殑鍐呭瓨涓崭细閲婃斁銆
寰阒熷垪鎻掑叆鏁版嵁镄勬椂鍊欙纴锲犱负涓岖煡阆撴槸钖︽湁娑堣垂钥呭勪簬绛夊緟鐘舵侊纴阃氱煡娑堣垂钥咃细
customerLock.notifyAll();//铏氭嫙链鸿皟搴︽秷璐硅呯嚎绋嬭繍琛岋纴瀹为檯涓婃槸镎崭綔绯荤粺锛屾妸淇濆瓨镄勬秷璐硅呯嚎绋嬬姸镐侊纴浠庢柊锷犺浇鍒瘫pu涓鎺ョ潃杩愯屻傛帴镌杩愯岀嚎绋嬫槸浠绘剰镄勶纴鍙栧喅浜庝笉钖屾搷浣灭郴缁熺殑绾跨▼璋冨害绠楁硶銆
娑堣垂钥呯嚎绋嬭诲彇涓涓鏁版嵁钖庯纴瑕侀氱煡鐢熶骇钥咃纴鍙浠ョ户缁锛岄亾鐞嗗悓涓婏细
makerLock.notifyAll();
阒熷垪涓锛屾棤鏁版嵁鍙璇荤殑镞跺欙细
customerLock.wait();//铡熺悊钖屼笂锛
链钖庢敞镒忥纴鐢熶骇钥呰窡娑堣垂钥呬娇鐢ㄤ简涓や釜涓嶅悓镄勫硅薄阌併俵ock.wait()镄勪娇鐢ㄦ柟娉曟槸杩欐牱镄勶细
synchronized(lock){
......
while(condition==true){
lock.wait();
}
......
Objecto=queen.pop();
lock.notifyAll();
}
链钖庡惎锷╪涓绾跨▼璇婚槦鍒楋纴妯℃嫙锷炵悊涓氩姟镄勭獥鍙o绂n涓绾跨▼鍐欓槦鍒楋纴妯℃嫙䦅呬汉鎺挜槦銆
鏂扮嚎绋嫔簱涔熸湁璺熻佺嚎绋嫔簱瀵瑰簲镄勬柟娉曪纴鏂扮嚎绋嫔簱链夌嚎绋嫔畨鍏ㄧ殑楂樻晥阒熷垪銆傛病链変笂闱㈤夯鐑︼纴浣嗕笂闱㈠啓镄勬槸鐞呜В鏂扮嚎绋嬫暟鎹缁撴瀯涓庡疄鐜扮殑锘虹銆
packagecom.;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Random;
publicclassTestThread2{
//缂揿啿涓婇檺
privatelongbufsize;
//缂揿啿
privateList<String>buf;
publicTestThread2(){
bufsize=5;
buf=newLinkedList<String>();
}
//鐢熶骇钥呰皟鐢
publicvoidput(Strings){
//妯℃嫙鐢熶骇钥呰窡涓崭笂娑堣垂钥
/*
try{
Thread.sleep(100);
}catch(InterruptedExceptione){
}
*/
synchronized(this){
//瓒呰繃阒熷垪闄愬埗灏辩瓑寰
while(buf.size()==bufsize){
System.out.println("阒熷垪宸叉弧锛岀敓浜ц:"+Thread.currentThread().getId()+"寮濮嬬瓑寰呫");
try{
this.wait();
}catch(InterruptedExceptione){
}
}
buf.add(s);
//阃氱煡娑堣垂钥
this.notifyAll();
}
}
//娑堣垂钥呰皟鐢
synchronizedpublicStringtake(){
//妯℃嫙娑堣垂钥呰窡涓崭笂鐢熶骇钥
try{
Thread.sleep(100);
}catch(InterruptedExceptione){
}
Strings=null;
synchronized(this){
while(buf.size()==0){
System.out.println("阒熷垪涓虹┖锛屾秷璐硅:"+Thread.currentThread().getId()+"寮濮嬬瓑寰呫");
try{
this.wait();
}catch(InterruptedExceptione){
}
}
//鍙栧厛鏀惧叆镄勫厓绱狅纴骞剁Щ闄
s=buf.get(0);
buf.remove(0);
//阃氱煡鐢熶骇钥
this.notifyAll();
}
returns;
}
publicstaticvoidmain(String[]args){
//镊宸卞疄鐜扮殑锛屽畨鍏ㄩ槦鍒
finalTestThread2tt=newTestThread2();
//鐢熶骇钥
Threadp=newThread(newRunnable(){
@Override
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
Randomr=newRandom();
tt.put(String.valueOf(r.nextInt(10)));
}
}
});
//娑堣垂钥
Threadc1=newThread(newRunnable(){
@Override
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
System.out.println("绾跨▼锛"+Thread.currentThread().getId()+"銮峰彇鍒版暟鎹"+tt.take());
}
}
});
Threadc2=newThread(newRunnable(){
@Override
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
System.out.println("绾跨▼锛"+Thread.currentThread().getId()+"銮峰彇鍒版暟鎹"+tt.take());
}
}
});
p.start();
c1.start();
c2.start();
try{
p.join();
c1.join();
c2.join();
}catch(InterruptedExceptione){
}
}
}
⑸ linux下多进程或者多线程编程的问题。新手,望指教!
你好,多进程或多线程,都不会阻塞当前语句代码。为了您的理解,我就大胆举下面两个例子:
多进程:你可以看成是本来是一条路的,现在从中间拆成两条,然后每一条路都有属于自己这条路的代码在运行。
多线程:你可以看成是一条路,然后分出车道,比如左车道和右车道甚至是停车道,然后每条车道都单独通车,其他车道的不能对这条车道进行干扰。
所以,把一条路从中间拆成两条,成本是很高的。但是把一条路分车道,成本就不是很高了。
对于您提出的main函数的疑问,当main函数最后执行完毕,程序退出后,所有的进程包括线程,都会被关闭的,哪怕你的程序中没有关闭,操作系统也会帮你关闭的,现在的操作系统都非常的完善了。当然,也存在有线程或进程不被释放的特殊情况,最好在编程中要记得释放。