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();
  }
}

相关文章

  • SpringBoot2.3集成ELK7.1.0的示例代码

    SpringBoot2.3集成ELK7.1.0的示例代码

    这篇文章主要介绍了SpringBoot2.3集成ELK7.1.0的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • 5分钟搞定java单例模式

    5分钟搞定java单例模式

    单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式,本文给大家介绍下java单例模式的相关知识,感兴趣的朋友一起看看吧
    2022-03-03
  • 基于SpringBoot和Vue3的博客平台发布、编辑、删除文章功能实现

    基于SpringBoot和Vue3的博客平台发布、编辑、删除文章功能实现

    在上一个教程中,我们已经实现了基于Spring Boot和Vue3的用户注册与登录功能。本教程将继续引导您实现博客平台的发布、编辑、删除文章功能,需要的朋友参考一下
    2023-04-04
  • SpringBoot使用POI进行Excel下载

    SpringBoot使用POI进行Excel下载

    这篇文章主要为大家详细介绍了SpringBoot使用POI进行Excel下载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • SpringBoot获取yml和properties配置文件的内容

    SpringBoot获取yml和properties配置文件的内容

    这篇文章主要为大家详细介绍了SpringBoot获取yml和properties配置文件的内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • SpringBoot中的WebSocketSession原理详解

    SpringBoot中的WebSocketSession原理详解

    这篇文章主要介绍了SpringBoot中的WebSocketSession原理详解,传统的 HTTP 协议是无法支持实时通信的,因为它是一种无状态协议,每次请求都是独立的,无法保持连接。为了解决这个问题,WebSocket 协议被引入,需要的朋友可以参考下
    2023-07-07
  • mybatisplus下划线驼峰转换的问题解决

    mybatisplus下划线驼峰转换的问题解决

    在mybatis-plus中,下划线-驼峰自动转换可能导致带下划线的字段查询结果为null,本文就来介绍一下mybatisplus下划线驼峰转换的问题解决,感兴趣的可以了解一下
    2024-10-10
  • Java处理科学计数法数字方式

    Java处理科学计数法数字方式

    这篇文章主要介绍了Java处理科学计数法数字方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • 对SpringBoot项目Jar包进行加密防止反编译

    对SpringBoot项目Jar包进行加密防止反编译

    最近项目要求部署到其他公司的服务器上,但是又不想将源码泄露出去,要求对正式环境的启动包进行安全性处理,防止客户直接通过反编译工具将代码反编译出来,本文介绍了如何对SpringBoot项目Jar包进行加密防止反编译,需要的朋友可以参考下
    2023-10-10
  • Java从源码角度解析SpringMVC执行流程

    Java从源码角度解析SpringMVC执行流程

    这篇文章主要介绍了Java从源码角度解析SpringMVC执行流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04

最新评论