线程和线程池的相关问题记录

来源:互联网 发布:北京市软件协会 编辑:程序博客网 时间:2024/06/09 15:55

卖票举例 thread的使用举例

//      MyThreadmt1 = new MyThread();

//      MyThreadmt2 = new MyThread();

//      MyThreadmt3 = new MyThread();

//      mt1.start();//每个线程都各卖了10张,共卖了30张票

//      mt2.start();//但实际只有10张票,每个线程都卖自己的票

//      mt3.start();//没有达到资源共享


输出结果,三个线程各卖了10张票,共30张



        MyThread1mt=new MyThread1();

        new Thread(mt).start();

        new Thread(mt).start();

        new Thread(mt).start();


输出结果,三个线程共卖了10张票,共10张

 

    class MyThreadextends Thread {

//      privateint ticket = 10;

//

//      publicvoid run() {

//          for(int i = 0; i < 20; i++) {

//              if(this.ticket > 0) {

//                  System.out.println("卖票:ticket" + this.ticket--);

//              }

//          }

//      }

    };

    class MyThread1implements Runnable{

        private int ticket = 10;

        public void run() {

            for (int i = 0; i < 20; i++){

                if (this.ticket > 0) {

                    System.out.println("卖票:ticket" +this.ticket--);

                }

            }

        }

    };

继承thread类线程内部的变量在各个线程间独立的(privateintticket = 10;)不共享的

实现runnable接口的类资源各个线程间是可以(private int ticket = 10;)共享的。

如果thread类线程和runnable接口中的资源(变量)是全局资源,那么都是共享的。

 

以下是四种线程池。

    ExecutorServicepool = Executors.newSingleThreadExecutor();

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。(顺序)

//  2.  ExecutorService pool =Executors.newCachedThreadPool();

创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。(并发)

//      3.ExecutorServicepool = Executors.newFixedThreadPool(1);

创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。(并发)

 

pool.execute((new MyThread()));

pool.execute((new MyThread1()));

pool.execute((new MyThread2()));

pool.execute((new MyThread3()));

   

 

4.创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下

 

1.      ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);  

2.     scheduledThreadPool.schedule(new Runnable() {  

3.      public void run() {  

4.       System.out.println("delay 3 seconds");  

5.      }  

6.     }, 3, TimeUnit.SECONDS);  

表示延迟3秒执行。

 

1.   ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);  

2.     scheduledThreadPool.scheduleAtFixedRate(new Runnable() {  

3.      public void run() {  

4.       System.out.println("delay 1 seconds, and excute every 3 seconds");  

5.      }  

6.     }, 13, TimeUnit.SECONDS);  

表示延迟1秒后每3秒执行一次。


    class MyThreadextends Thread implements Runnable{

        private int ticket = 0;

//

        public void run() {

//          for(int i = 0; i < 20; i++) {

//              if(this.ticket > 0) {

                    Log.e("aaa","卖票:ticket" +this.ticket);

                    System.out.println("卖票:ticket" +this.ticket);

                }

//          }

//      }

    };

    class MyThread1extends Thread implements Runnable{

        private int ticket = 1;

//

        public void run() {

//          for(int i = 0; i < 20; i++) {

//              if(this.ticket > 0) {

                    Log.e("aaa","卖票:ticket" +this.ticket);

                    System.out.println("卖票:ticket" +this.ticket);

                }

//          }

//      }

    };

    class MyThread2extends Thread implements Runnable{

        private int ticket = 2;

//

        public void run() {

//          for(int i = 0; i < 20; i++) {

//              if(this.ticket > 0) {

            Log.e("aaa","卖票:ticket" +this.ticket);

                    System.out.println("卖票:ticket" +this.ticket);

                }

//          }

//      }

    };

    class MyThread3extends Thread implements Runnable{

        private int ticket = 3;

//

        public void run() {

//          for(int i = 0; i < 20; i++) {

//              if(this.ticket > 0) {

            Log.e("aaa","卖票:ticket" +this.ticket);

                    System.out.println("卖票:ticket" +this.ticket);

                }

//          }

//      }

    };


0 0