您的位置:首页 > 编程语言 > Java开发

java看看我是怎么利用数组给Runnable线程传参数的1

2013-09-07 18:15 351 查看
我的文章只给有耐心的人看,所以先写代码

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() {

        return service;

    }

    public void setService(ExecutorService service) {

        this.service = service;

    }

    public Semaphore getSp() {

        return sp;

    }

    public void setSp(Semaphore sp) {

        this.sp = sp;

    }

    public int getNt() {

        return nt;

    }

    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();

            TestWait t = new TestWait();

            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("--------->thread No. [" + nt + "]  start ! ***** 还有 " +sp.getQueueLength()+" 线程等待执行");

            

            

            try {

                Thread.sleep(2000);//9000); //1000);

            } catch (InterruptedException e1) {

                e1.printStackTrace();

            }

        

        try{

//            线程减数

            sp.release();

        }catch(Exception e)

        {

            e.printStackTrace();

        }

        

    }
}

问题来源于给Runnable 接口的线程传参数……

多个线程,传进去的参数全不一样……

比如进去的、出来的……

先看个(游戏)参与者类:

public class Playe2r {

public     int id;

//    String name;

    int state; //1 可以对弈

    

    public Playe2r( int i, int j) {

        id=i;

        //name=nam1e;

        state=j;

        // TODO Auto-generated constructor stub

    }

    void setId(int i1d ) { this.id= i1d;}

    int getId() {return this.id;}

}

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class player1test1 {

    

    public static void main(String[] args)

    {

        Playe1r p1[]=new Playe1r[1000];

        for(int i=0;i<p1.length; ++i) {

            p1[i]=new Playe1r(i,""+i+"",1);

//            p1[i].setId(i);

            p1[i].id=i;

            p1[i].state=(int) (9*Math.random());

        }

        

        for(int i= (p1.length-1); i>0; --i) {

            p1[i].getId();

            System.out.print(p1[i].id);

            System.out.print(":");

            System.out.println(p1[i].state);

        }

        System.out.println();

        

        //--

        List lst1=new ArrayList();

        

        for(int i1=0;i1<p1.length;++i1) {

            if(  (1<p1[i1].state )&&(4>p1[i1].state) ) {

                lst1.add( new Playe2r(p1[i1].getId(), p1[i1].state) );

            }//if(1==p1[i1].getId(i1

        }//for(int i1

        

        Iterator it = lst1.iterator();

        while(it.hasNext()){

          // String s = (String)it.next(). ;

            Object obj=it.next();

            Playe2r objj=(Playe2r) obj;

           System.out.print(objj.getId() );

           System.out.print(" ");

           System.out.println(objj.state);

        }

        

        //==

        

        //-某player1 发起邀请

        

        for (int i1i=1;i1i<99;++i1i) {

            int rami=lst1.size();

            int no_i=(int) (rami*(Math.random()));

            

            for (int j1=1;j1<rami;++j1) {

                if (no_i!=j1) {

                    //把  (no_i, j1) 这两个参数 分别 传给  邀请的 TestWait 线程(编号为响应的 线程池中的线程……

                }//if (no_i!=j1            

            }//for(int j1=1

            

        }//for int i1i=1

        

        //==

        

        

    }//main

    

}//class player1test1

主要的程序就是以上的部分……

至于 利用 “数组”如何传 相应(编号)的参数进去,咱们下回再说(文章已经够长了……)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐