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();
     
  • 相关阅读:
    初学OptaPlanner-01- 什么是OptaPlanner?
    初学推荐系统-05-Wide&Deep [附tensorflow的WideDeepModel代码简单实践]
    初学推荐系统-04-FM (因子分解机:多特征的二阶特征交叉)
    初学推荐系统-03- 隐语义模型与矩阵分解
    初学推荐系统-02-协同过滤 (UserCF & ItermCF) -附简单示例和优缺点分析
    [Datawhale 10月] 初学推荐系统-01-概述
    TiDB-BR数据备份和恢复工具
    Oracle-估算运行时间长的耗时操作语句
    Hadoop、Spark——完全分布式HA集群搭建
    Hadoop——集群参数配置详解
  • 原文地址:https://www.cnblogs.com/candycloud/p/4447215.html
Copyright © 2011-2022 走看看