多线程程序

多线程程序

  1. Java在语言级提供了对多线程程序设计的支持
  2. 实现多线程程序的两种方式:
    1. 从Thread类继承;(file 1,file2)
    2. 实现Runnable接口;(file 3) 大多数情况下,如果只想重写 run() 方法,而不重写其他 Thread 方法,那么应使用 Runnable 接口;
  3. Java运行时系统实现了一个用于调度线程执行的线程调度器,用于确定某一时刻由哪一个线程在CUP上运行;
  4. 在Java技术中,线程通常是抢占式的而不需要时间片分配进程。抢占式调度模型就是许多线程处于可以运行状态(等待状态),但实际上只有一个线程在运行,该线程一直运行到它终止进入可运行状态(等待状态),或者另一个具有更高优先级的线程变成可运行状态。在后一种情况下,低优先级的线程被高优先级的线程强占,高优先级的线程获得运行的机会;
  5. Java线程调度器支持不同优先级线程的抢先方式,但其本身不支持相同优先级线程的时间片轮换;
  6. Java运行时系统所在的操作系统支持时间片的轮换,则线程调度器就支持相同优先级线程的时间片轮换;

## file 1 ##
class MulThread{
    public static void main(String[] args){
        MyThread mt = new MyThread();
        mt.start();

        System.out.println("main="+Thread.currentThread().getName());// main函数线程的名字;
        // 运行结果:main Thread-0;说明main函数先分配了一个时间片,结束后才是MyThread;可以用循环打印看看;
    }
}

class MyThread extends Thread{
    // 定义一个类,就是开了一个线程;
    //创建新执行线程有两种方法。一种方法是将类声明为 Thread 的子类。该子类应重写 Thread 类的 run 方法
    public void run(){
        System.out.println(getName());
    }
}
## file 2 ##
class MulThread{
    public static void main(String[] args){
        MyThread mt = new MyThread();
//将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java 虚拟机退出。 
//该方法必须在启动线程前调用。 

        mt.setDaemon(true);
        mt.start();     
        int index = 0;
        while(true){
            if(index++ == 1000){
                break;
            }
            System.out.println("main="+Thread.currentThread().getName());
        }


    }
}

class MyThread extends Thread{
    public void run(){  
        while(true)
            System.out.println(getName());
    }
}
## file 3 ##
class MulThread{
    public static void main(String[] args){
        MyThread mt = new MyThread();   
        Thread t = new Thread(mt);
        t.setDaemon(true);
        t.start();
        int index=0;
        while(true){
            if(index++==1000)
                break;
            System.out.println("main="+Thread.currentThread().getName());

        }


    }
}

class MyThread implements Runnable{
    public void run(){  
        while(true){            
            System.out.println(Thread.currentThread().getName());

        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多线程 求质数 返回数组中的最大值 bool isPrime(long x) { if (x <= 1) return false; if (x == 2) return true; for (long i = 2; i <= ceil(sqrt((long double)x));i++) if (x%i == 0) return false; return true; } DWORD WINAPI findPrime(void* p) { long result=0; int l = stc(p)->lower, u = stc(p)->uper; int t_id=GetCurrentThreadId(); for (int i = l; i <= u;i++) if (isPrime(i)) { result++; } DWORD dReturn = WaitForSingleObject(mutex_mul_result_h, INFINITE); mul_result += result; ReleaseMutex(mutex_mul_result_h); //EnterCriticalSection(&gCRITICAL_SECTION_Printf); //printf("%d,%d,%d,%d\n", l, u, result,t_id); //fflush(stdout); //LeaveCriticalSection(&gCRITICAL_SECTION_Printf); return 0; } //dispatcher void dispatch() { DWORD Status; timer tm; tm.start(); //srand(time(NULL)); long step = STEP;//ceil(double(TEST/10)); handlenum = 0; for (int i = 1; i <= TEST;) { i += step; handlenum++; } handle_array=new HANDLE[handlenum]; Thread_id = new DWORD[handlenum ]; arg = new FP_ARG[handlenum]; InitializeCriticalSection(&gCRITICAL_SECTION_Printf); mutex_mul_result_h = CreateMutex(NULL, false, mutex_mul_result); handlenum = 0; for (int i = 1; i <= TEST;) { arg[handlenum].lower = i; arg[handlenum].uper = (i + step-1>TEST) ? TEST : i + step-1; handle_array[handlenum]=(HANDLE)CreateThread(NULL, 0, findPrime, &arg[handlenum], 0, &Thread_id[handlenum]); /*EnterCriticalSection(&gCRITICAL_SECTION_Printf); printf("lower:%d uper:%d thread_id:%d\n", arg[handlenum].lower, arg[handlenum].uper,Thread_id[handlenum]); LeaveCriticalSection(&gCRITICAL_SECTION_Printf);*/ i += step; handlenum++; } tm.stop(); Sleep(1000); printf("there are %d threads\n", handlenum); printf("the multithreads use %f msc\n", tm.read()); } //the number of 1-1000000 Primenumber void s_finePrime() { timer tm; long result = 0; tm.start(); for (int i = 1; i <= TEST; i++) if (isPrime(i)) result++; tm.stop(); printf("Single thread result is %d\n", result); printf("Single thread use %f msc\n", tm.read()); } int _tmain(int argc, _TCHAR* argv[]) { dispatch(); WaitForMultipleObjects(handlenum, handle_array, true, INFINITE);//不起作用 printf("the multithreads reslut is %d\n", mul_result); CloseHandle(mutex_mul_result_h); DeleteCriticalSection(&gCRITICAL_SECTION_Printf); s_finePrime(); system("pause"); return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值