多线程传参及多线程并发量控制

来源:互联网 发布:冰毒淘宝地址 编辑:程序博客网 时间:2024/05/16 15:43

多线程传参及多线程并发量控制

多线程传参

多线程传参的控制有两种:

假设有线程类如下:

Public class MyThread

{

         privateint arg1;

         pirvateString arg2;

        

         Setters;

         Getters;

 

pubic MyThread()

{

         super();

}

 

 

publicMuThread(int ar1,String ar2)

{

         this.arg1 = ar1;

         this.arg2 = ar2;

}

}

      1.构造方法

         通过在新建线程的时候,将参数传入到子线程构造函数中

         例如:

for(int I = 0 ; I < 100 ; I++)

{

         MyThreadmt = new MyThread(参数1,参数2);

         Thread td = new Thread(t);

    Td.start();

}

 

      2.属性设置

         通过在新建线程的时候,将参数Set到子线程的属性中

         例如:

for(int I = 0 ; I < 100 ; I++)

{

         MyThread mt =new MyThread();

         mt.setArg1(参数1);

         mt.setArg2(参数2);

         Thread td =new Thread(t);

    td.start();

}

 

并发量控制

并发量的控制可以通过java中的并发信号量来控制

示例代码

importjava.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class TestSemaphore {

 public static void main(String[]args) {
  //
线程池
  ExecutorService exec = Executors.newCachedThreadPool();

  // 只能个线程同时访问
  final Semaphore semp = new Semaphore(5);
  for (int index = 1; index < 21; index++) {
   final int NO = index;
   Runnable run = new Runnable() {
    public void run() {
     try {
      //
获取许可
      semp.acquire();
      System.out.println("Accessing: "+ NO);
      
      Thread.sleep((long) (Math.random() *1000));

      // 访问完后,释放
      System.out.println("Release: " +NO);
      semp.release();
     } catch (InterruptedException e) {

     }
    }

   };
   exec.execute(run);
  }

  // 退出线程池
  exec.shutdown();
 }
}

 

 

混合代码代码

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

 

 

 

public class TestWait implements Runnable {

//  当前信号量

    int nt = 0;

//  线程池

    ExecutorService service = null;

//  全局信号量

    Semaphore sp = null;

   

   

    public ExecutorService getService() {

       returnservice;

    }

 

    public void setService(ExecutorService service) {

       this.service = service;

    }

 

    public Semaphore getSp() {

       returnsp;

    }

 

    public void setSp(Semaphore sp) {

       this.sp = sp;

    }

 

    public int getNt() {

       returnnt;

    }

 

    public void setNt(int nt) {

       this.nt = nt;

    }

 

    public static void main(String[] args) {

//     线程池

       ExecutorService tservice = Executors.newCachedThreadPool();

//     设定多线程并发信号量

       final Semaphore tsp =new Semaphore(10);

      

       int cnt = 100;

       for (int i = 1; i <= cnt; i++) {

          

           System.out.println("add the No.[" + i +"] thread in thread serveice!");

           Test t = new Test();

           t.setNt(i);

           t.setService(tservice);

           t.setSp(tsp);

          

           Thread td = new Thread(t);

          

           tservice.execute(td);

          

       }

       tservice.shutdown();

    }

 

    public void run() {

       try{

//         线程加数

           sp.acquire();

       }

       catch(Exception e)

       {

           e.printStackTrace();

       }

      

           System.out.println("--------->threadNo. [" +nt + "]  start ! *****还有 " +sp.getQueueLength()+"线程等待执行");

          

          

           try {

              Thread.sleep(1000);

           } catch (InterruptedException e1) {

              e1.printStackTrace();

           }

      

       try{

//         线程减数

           sp.release();

       }catch(Exception e)

       {

           e.printStackTrace();

       }

      

    }

}

 

 

0 0
原创粉丝点击