Java并发程序入门介绍

 更新时间:2015年03月26日 21:11:51   作者:Microgoogle  
这篇文章主要介绍了Java并发程序入门 ,需要的朋友可以参考下

今天看了看Java并发程序,写一写入门程序,并设置了线程的优先级。

class Elem implements Runnable{
  public static int id = 0;
  private int cutDown = 5;
  private int priority;
  
  public void setPriority(int priority){
    this.priority = priority;
  }
  
  public int getPriority(){
    return this.priority;
  }
  public void run(){
    Thread.currentThread().setPriority(priority);
    int threadId = id++;
    while(cutDown-- > 0){
      double d = 1.2;
      while(d < 10000)
        d = d + (Math.E + Math.PI)/d;
      System.out.println("#" + threadId + "(" + cutDown + ")");
    }
  }
}
public class Basic {
  public static void main(String args[]){
    for(int i = 0; i < 10; i++){
      Elem e = new Elem();
      if(i == 0 )
        e.setPriority(Thread.MAX_PRIORITY);
      else
        e.setPriority(Thread.MIN_PRIORITY);
      Thread t = new Thread(e);
      t.start();
    }
  }
}

由于机器很强悍,所以先开始并没看到并发的效果,感觉是按顺序执行的,所以在中间加了浮点数的运算来延迟时间。

当然,main函数里面可以用Executors来管理线程。

import java.util.concurrent.*;
class Elem implements Runnable{
  public static int id = 0;
  private int cutDown = 5;
  private int priority;
  
  public void setPriority(int priority){
    this.priority = priority;
  }
  
  public int getPriority(){
    return this.priority;
  }
  public void run(){
    Thread.currentThread().setPriority(priority);
    int threadId = id++;
    while(cutDown-- > 0){
      double d = 1.2;
      while(d < 10000)
        d = d + (Math.E + Math.PI)/d;
      System.out.println("#" + threadId + "(" + cutDown + ")");
    }
  }
}
public class Basic {
  public static void main(String args[]){
//    for(int i = 0; i < 10; i++){
//      Elem e = new Elem();
//      if(i == 0 )
//        e.setPriority(Thread.MAX_PRIORITY);
//      else
//        e.setPriority(Thread.MIN_PRIORITY);
//      Thread t = new Thread(e);
//      t.start();
//    }
    ExecutorService exec = Executors.newCachedThreadPool();
    for(int i = 0; i < 10; i++){
      Elem e = new Elem();
      if(i == 0 )
        e.setPriority(Thread.MAX_PRIORITY);
      else
        e.setPriority(Thread.MIN_PRIORITY);
      exec.execute(e);
    }
    exec.shutdown();
  }
}

相关文章

  • Spring Boot使用profile如何配置不同环境的配置文件

    Spring Boot使用profile如何配置不同环境的配置文件

    ,springboot支持通过不同的profile来配置不同环境的配置,下面就大致介绍一下yml配置文件跟properties配置文件怎么使用profile配置不同环境的配置文件
    2018-01-01
  • 使用spring整合Quartz实现—定时器功能

    使用spring整合Quartz实现—定时器功能

    这篇文章主要介绍了使用spring整合Quartz实现—定时器功能,不基于特定的基类的方法,需要的朋友可以参考下
    2018-04-04
  • 简单了解Java多线程实现的四种方式

    简单了解Java多线程实现的四种方式

    这篇文章主要介绍了简单了解Java多线程实现的四种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Spring Boot 实现配置文件加解密原理

    Spring Boot 实现配置文件加解密原理

    这篇文章主要介绍了Spring Boot 实现配置文件加解密原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • SpringBoot SSE服务端主动推送事件的实现

    SpringBoot SSE服务端主动推送事件的实现

    本文主要介绍了SpringBoot SSE服务端主动推送事件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java获取当前系统事件System.currentTimeMillis()方法

    Java获取当前系统事件System.currentTimeMillis()方法

    下面小编就为大家带来一篇Java获取当前系统事件System.currentTimeMillis()方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java 内部类的定义与范例

    Java 内部类的定义与范例

    说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉。原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法。今天我们就来一探究竟
    2021-11-11
  • 如何优雅的抛出Spring Boot注解的异常详解

    如何优雅的抛出Spring Boot注解的异常详解

    这篇文章主要给大家介绍了关于如何优雅的抛出Spring Boot注解的异常的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • MyBatis实现批量插入方法实例

    MyBatis实现批量插入方法实例

    最近在公司项目开发中遇到批量数据插入或者更新,下面这篇文章主要给大家介绍了关于MyBatis实现批量插入的相关资料,需要的朋友可以参考下
    2022-10-10
  • Java多线程并发开发之DelayQueue使用示例

    Java多线程并发开发之DelayQueue使用示例

    这篇文章主要为大家详细介绍了Java多线程并发开发之DelayQueue使用示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09

最新评论