package Thread.Abort;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; //计数类
class Count {//DATA
private int count = 0;
private Random rand = new Random(47); //
public synchronized int increment() {
int temp = count;
if (rand.nextBoolean())
Thread.yield();
return (count = ++temp);
} public synchronized int value() {
return count;
}
} //动作类
class Entrance implements Runnable {
private static Count count = new Count();
private static List<Entrance> entrances = new ArrayList<Entrance>();
private int number = 0;
private final int id;
private static volatile boolean canceled = false; public static void cancel() {
canceled = true;
} public Entrance(int id) {
this.id = id;
entrances.add(this);
} @Override
public void run() {
while (!canceled) {
synchronized (this) {
++number;
}
System.out.println(this + " Total:" + count.increment());//要显示的数值都要通过同步方法读取才准确
try {
TimeUnit.MICROSECONDS.sleep(100);
} catch (Exception e) {
System.out.println("sleep interrupted");
}
}
System.out.println("Stopping " + this);
} public synchronized int getValue() {
return number;
} public String toString() {
return "Entrance " + id + ": " + getValue();
} public static int getTotalCount() {
return count.value();
} public static int sumEntrances() {
int sum = 0;
for (Entrance entrance : entrances) {
sum += entrance.getValue();
}
return sum;
}
} public class OrnamentalGarden {
public static void main(String[] args) throws InterruptedException {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
exec.execute(new Entrance(i));
}
TimeUnit.SECONDS.sleep(3);
Entrance.cancel();
exec.shutdown();
if (!exec.awaitTermination(250, TimeUnit.MILLISECONDS))
System.out.println("Some tasks were not terminated!");
System.out.println("Total:" + Entrance.getTotalCount());
System.out.println("Sum of Entrances:" + Entrance.sumEntrances());
}
}

java多线程之计算数量的更多相关文章

  1. java多线程(精华版)

    在 Java 程序中使用多线程要比在 C 或 C++ 中容易得多,这是因为 Java 编程语言提供了语言级的支持.本文通过简单的编程示例来说明 Java 程序中的多线程是多么直观.读完本文以后,用户应 ...

  2. Java多线程编程——进阶篇二

    一.线程的交互 a.线程交互的基础知识 线程交互知识点需要从java.lang.Object的类的三个方法来学习:    void notify()           唤醒在此对象监视器上等待的单个 ...

  3. Java多线程学习(转载)

    Java多线程学习(转载) 时间:2015-03-14 13:53:14      阅读:137413      评论:4      收藏:3      [点我收藏+] 转载 :http://blog ...

  4. JAVA多线程下载网络文件

    JAVA多线程下载网络文件,开启多个线程,同时下载网络文件.   源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...

  5. 【转】 Java 多线程之一

    转自   Java 多线程 并发编程 一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进 ...

  6. 从JAVA多线程理解到集群分布式和网络设计的浅析

    对于JAVA多线程的应用非常广泛,现在的系统没有多线程几乎什么也做不了,很多时候我们在何种场合如何应用多线程成为一种首先需要选择的问题,另外关于java多线程的知识也是非常的多,本文中先介绍和说明一些 ...

  7. Java多线程详解

    Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程 ...

  8. java 多线程 一个博客

    http://blog.csdn.net/a352193394/article/category/2563875 Java多线程之~~~线程安全容器的非阻塞容器 在并发编程中,会经常遇到使用容器.但是 ...

  9. (转载)Java多线程入门理解

    转载出处http://blog.csdn.net/evankaka 写在前面的话:此文只能说是java多线程的一个入门,其实Java里头线程完全可以写一本书了,但是如果最基本的你都学掌握好,又怎么能更 ...

随机推荐

  1. 论文阅读之 A Convex Optimization Framework for Active Learning

    A Convex Optimization Framework for Active Learning Active learning is the problem of progressively ...

  2. 在CentOS上安装Python

    首先我们需要在服务器上安装一个比较新的 Python,CentOS 5.8 默认装的 Python 是 2.4.3. [root@nowamagic ~]# python -V Python 我们需要 ...

  3. form表单回车提交问题,JS监听回车事件

    我们有时候希望回车键敲在文本框(input element)里来提交表单(form),但有时候又不希望如此.比如搜索行为,希望输入完关键词之后直接按回车键立即提交表单,而有些复杂表单,可能要避免回车键 ...

  4. unity, 搜索组件

    Hierarchy的搜索栏中既可以搜节点名,也可以搜组件名.

  5. c#, 输出二进制

    int x=-17; string str= Convert.ToString(x,2);Debug.Log(str); 输出结果: 11111111111111111111111111101111 ...

  6. noip2012普及组——质因数分解

    [问题描述]已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. [输入]输入文件名为 prime.in.输入只有一行,包含一个正整数 n. [输出]输出文件名为 prime.out.输出只 ...

  7. 【转】ASP.NET服务器控件使用之MultiView和View

    MultiView 和 View 控件和制作出选项卡的效果,MultiView 控件是一组 View 控件的容器.使用它可定义一组 View 控件,其中每个 View 控件都包含子控件. 如果要切换视 ...

  8. Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom

    一.视图切换类型介绍 在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的. 在iphone中,segue有:push,modal,和cus ...

  9. Hadoop数据传输工具:Sqoop

    Apache Sqoop(SQL-to-Hadoop) 项目旨在协助 RDBMS 与 Hadoop 之间进行高效的大数据交流.用户可以在 Sqoop 的帮助下,轻松地把关系型数据库的数据导入到 Had ...

  10. C#颜色和名称样式对照表

    1   Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.L ...