zoukankan      html  css  js  c++  java
  • java线程基础(一些常见多线程用法)

    1.
    继承Runnable的用法:
    关键是继承Runnable并且必须实现run()函数,通过run函数启动线程,似乎多个线程不可以并行
     
    TestRunnable.java
    package TestPackage;

    import java.lang.Runnable;

    package TestPackage;
     
    import java.lang.Runnable;
     
    public class TestRunnable implements Runnable {
            public void run() {
                   // TODO Auto-generated method stub
                  System. out.println("hello world1 !" );
                   try {
                         Thread. sleep(1000);
                  } catch (InterruptedException ) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                  }
                  System. out.println("hello world2 !" );
           }
    }
    使用语句:
           TestRunnable thread new TestRunnable();
           TestRunnable thread1 new TestRunnable();
           thread.run();
           thread1.run();
    2.
    继承Thread方法
    继承Thread方法之后,实现run()函数,创建该对象运行start()函数即可启动线程,多个线程可以并行
     
    TestThread.java
    package TestPackage;
     
    public class TestThread extends Thread {
            public void run() {
                  System. out.println("hello Thread!" );
                   try {
                         Thread. sleep(2000);
                  } catch (InterruptedException ) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                  }
                  System. out.println("hello Thread!" );
           }
    }
     
    使用语句:
            TestThread tt new TestThread();
            TestThread tt1 new TestThread();
            tt.start();
            tt1.start();
    3.
    带任务队列的线程模型
    这是生产者消费者模式,生产者消费者都继承Thread,然后共享一个阻塞队列(BlockingQueue)
    TestBlockingQueue.java
     
    package TestPackage;
     
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
     
    public class TestBlockingQueue {
            public static void runConsumerProducer() {
                  BlockingQueue<Integer> =  new LinkedBlockingQueue<Integer>(10);
                  Producer producer new Producer();
                  Consumer consumer1 new Consumer(q);
                  Consumer consumer2 new Consumer(q);
                   producer.start();
                   consumer1.start();
                   consumer2.start();
           }
    }
     
    class Producer extends Thread{
            final private BlockingQueue<Integer> taskQueue;
            public Producer(BlockingQueue<Integer> ) {
                   taskQueue ;
           }
            public void run() {
                   int = 0;
                   while (true ) {
                          try {
                               System. out.println("put " );
                                taskQueue.put();
                         } catch (InterruptedException ) {
                                // TODO Auto-generated catch block
                                e. printStackTrace();
                         }
                         ++ i;
                          try {
                               Thread. sleep(100);
                         } catch (InterruptedException ) {
                                // TODO Auto-generated catch block
                                e. printStackTrace();
                         }
                  }
           }
    }
     
    class Consumer extends Thread {
            final private BlockingQueue<Integer> taskQueue;
            public Consumer(BlockingQueue<Integer> ) {
                   taskQueue ;
           }
            public void run() {
                   while (true ) {
                          try {
                               System. out.println("get :" taskQueue .take());
                         } catch (InterruptedException ) {
                                // TODO Auto-generated catch block
                                e. printStackTrace();
                         }
                          try {
                               Thread. sleep(1000);
                         } catch (InterruptedException ) {
                                // TODO Auto-generated catch block
                                e. printStackTrace();
                         }
                  }
           }      
    }
     
    使用语句:
    TestBlockingQueue.runConsumerProducer();
     
  • 相关阅读:
    【IIS错误】IIS各种错误
    【IIS错误
    【C#】C#操作Excel文件(转)
    【C#】语音识别
    【IIS错误】未能加载文件或程序集“AAAAA”或它的某一个依赖项。试图加载格式不正确的程序。
    【Web前端】清除css、javascript及背景图在浏览器中的缓存
    【模态窗口-Modeldialog】提交请求时禁止在新窗口打开页面的处理方法
    第八周学习进度表
    梦断代码阅读笔记01
    第二阶段冲刺第七天
  • 原文地址:https://www.cnblogs.com/candycloud/p/4447215.html
Copyright © 2011-2022 走看看