• 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();
     
  • 相关阅读:
    EditPlus删除空行、空段落的正则表达式
    composer 2 install 或 update时报错解决
    001-接入腾讯云物联网通信
    004-STM32+ESP8266+AIR202基本控制篇-功能测试-Android扫码绑定Air202(GPRS)并通过阿里云物联网平台实现通信控制
    STM32+ESP8266+AIR202基本控制篇-315-功能测试-Air202(GPRS)以SSL单向认证方式连接MQTT服务器(不校验服务器证书)
    003-STM32+ESP8266+AIR202基本控制方案-功能测试-Android使用SmartConfig配网绑定ESP8266并通过阿里云物联网平台和ESP8266实现通信控制
    002-STM32+ESP8266+AIR202基本控制方案-关于阿里云物联网平台上的自定义,物模型,基础通信Topic使用说明
    002-STM32+ESP8266+AIR202基本控制方案-功能测试-Android使用APUConfig配网绑定ESP8266并通过阿里云物联网平台和ESP8266实现通信控制
    4.1-Air302(NB-IOT)-自建MQTT服务器-购买云服务器安装MQTT服务器软件(Linux系统)
    4.1-Air302(NB-IOT)-自建MQTT服务器-购买云服务器安装MQTT服务器软件(Windows系统)
  • 原文地址:https://www.cnblogs.com/candycloud/p/4447215.html
Copyright © 2020-2023  润新知