楼主: ReneeBK
1106 6

Java 9 Concurrency Cookbook - Second Edition [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4900份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49655 个
通用积分
55.9937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-6-9 08:08:24 |AI写论文
1论坛币

关键词:Cookbook currency Edition Second editio Java

本帖被以下文库推荐

沙发
ReneeBK 发表于 2017-6-9 08:11:06
  1. package com.packtpub.java9.concurrency.cookbook.chapter01.recipe01.main;

  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.lang.Thread.State;

  6. import com.packtpub.java9.concurrency.cookbook.chapter01.recipe01.task.Calculator;

  7. /**
  8. * Main class of the example
  9. */
  10. public class Main {

  11.         /**
  12.          * Main method of the example
  13.          *
  14.          * @param args
  15.          */
  16.         public static void main(String[] args) {

  17.                 // Thread priority infomation
  18.                 System.out.printf("Minimum Priority: %s\n", Thread.MIN_PRIORITY);
  19.                 System.out.printf("Normal Priority: %s\n", Thread.NORM_PRIORITY);
  20.                 System.out.printf("Maximun Priority: %s\n", Thread.MAX_PRIORITY);

  21.                 Thread threads[];
  22.                 Thread.State status[];

  23.                 // Launch 10 threads to do the operation, 5 with the max
  24.                 // priority, 5 with the min
  25.                 threads = new Thread[10];
  26.                 status = new Thread.State[10];
  27.                 for (int i = 0; i < 10; i++) {
  28.                         threads[i] = new Thread(new Calculator());
  29.                         if ((i % 2) == 0) {
  30.                                 threads[i].setPriority(Thread.MAX_PRIORITY);
  31.                         } else {
  32.                                 threads[i].setPriority(Thread.MIN_PRIORITY);
  33.                         }
  34.                         threads[i].setName("My Thread " + i);
  35.                 }

  36.                 // Wait for the finalization of the threads. Meanwhile,
  37.                 // write the status of those threads in a file
  38.                 try (FileWriter file = new FileWriter(".\\data\\log.txt"); PrintWriter pw = new PrintWriter(file);) {

  39.                         // Write the status of the threads
  40.                         for (int i = 0; i < 10; i++) {
  41.                                 pw.println("Main : Status of Thread " + i + " : " + threads[i].getState());
  42.                                 status[i] = threads[i].getState();
  43.                         }

  44.                         // Start the ten threads
  45.                         for (int i = 0; i < 10; i++) {
  46.                                 threads[i].start();
  47.                         }

  48.                         // Wait for the finalization of the threads. We save the status of
  49.                         // the threads and only write the status if it changes.
  50.                         boolean finish = false;
  51.                         while (!finish) {
  52.                                 for (int i = 0; i < 10; i++) {
  53.                                         if (threads[i].getState() != status[i]) {
  54.                                                 writeThreadInfo(pw, threads[i], status[i]);
  55.                                                 status[i] = threads[i].getState();
  56.                                         }
  57.                                 }

  58.                                 finish = true;
  59.                                 for (int i = 0; i < 10; i++) {
  60.                                         finish = finish && (threads[i].getState() == State.TERMINATED);
  61.                                 }
  62.                         }

  63.                 } catch (IOException e) {
  64.                         e.printStackTrace();
  65.                 }
  66.         }

  67.         /**
  68.          * This method writes the state of a thread in a file
  69.          *
  70.          * @param pw
  71.          *            : PrintWriter to write the data
  72.          * @param thread
  73.          *            : Thread whose information will be written
  74.          * @param state
  75.          *            : Old state of the thread
  76.          */
  77.         private static void writeThreadInfo(PrintWriter pw, Thread thread, State state) {
  78.                 pw.printf("Main : Id %d - %s\n", thread.getId(), thread.getName());
  79.                 pw.printf("Main : Priority: %d\n", thread.getPriority());
  80.                 pw.printf("Main : Old State: %s\n", state);
  81.                 pw.printf("Main : New State: %s\n", thread.getState());
  82.                 pw.printf("Main : ************************************\n");
  83.         }

  84. }
复制代码

藤椅
Nicolle 学生认证  发表于 2018-11-28 03:00:51
提示: 作者被禁止或删除 内容自动屏蔽

板凳
Nicolle 学生认证  发表于 2018-11-28 03:01:34
提示: 作者被禁止或删除 内容自动屏蔽

报纸
Nicolle 学生认证  发表于 2018-11-28 03:02:16
提示: 作者被禁止或删除 内容自动屏蔽

地板
millionaire 发表于 2018-12-19 11:54:40
here

Java_9_Concurrency_Cookbook.pdf
下载链接: https://bbs.pinggu.org/a-2680747.html

5.96 MB

需要: 20 个论坛币  [购买]

7
eileen666 发表于 2019-1-21 00:09:48
millionaire 发表于 2018-12-19 11:54
here
thanks

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-22 18:05