Java: Thread
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /** * encoding: utf-8 * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2023 - 2023/12/16 - 16:40 * # User : geovindu * # Product : IntelliJ IDEA * # Project : javademo * # File : Calculator.java 类 * # explain : 学习 **/ package Concurrency; import java.lang.*; import java.util.*; /** * This class prints the multiplication table of a number *Concurrency * https://github.com/PacktPublishing/Java-9-Concurrency-Cookbook-Second-Edition * Java 9 Concurency Cookbook by Javier Fernandez Gonzalez */ public class Calculator implements Runnable { /** * Method that do the calculations */ @Override public void run() { long current = 1L; long max = 20000L; long numPrimes = 0L; System.out.printf( "Thread '%s': START\n" , Thread.currentThread().getName()); while (current <= max) { if (isPrime(current)) { numPrimes++; } current++; } System.out.printf( "Thread '%s': END. Number of Primes: %d\n" , Thread.currentThread().getName(), numPrimes); } /** * Method that calculate if a number is prime or not * * @param number * : The number * @return A boolean value. True if the number is prime, false if not. */ private boolean isPrime( long number) { if (number <= 2 ) { return true ; } for ( long i = 2 ; i < number; i++) { if ((number % i) == 0 ) { return false ; } } return true ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | /** * encoding: utf-8 * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2023 - 2023/12/16 - 16:41 * # User : geovindu * # Product : IntelliJ IDEA * # Project : javademo * # File : CalculatorBLL.java 类 * # explain : 学习 **/ package BLL; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.Thread.State; import Concurrency.Calculator; public class CalculatorBLL { /** * 线程 Concurrency 并行编程 * https://github.com/PacktPublishing/Java-9-Concurrency-Cookbook-Second-Edition */ public static void CalculatorExample() { // Thread priority infomation System.out.printf( "Minimum Priority: %s\n" , Thread.MIN_PRIORITY); System.out.printf( "Normal Priority: %s\n" , Thread.NORM_PRIORITY); System.out.printf( "Maximun Priority: %s\n" , Thread.MAX_PRIORITY); Thread threads[]; Thread.State status[]; // Launch 10 threads to do the operation, 5 with the max // priority, 5 with the min threads = new Thread[ 10 ]; status = new Thread.State[ 10 ]; for ( int i = 0 ; i < 10 ; i++) { threads[i] = new Thread( new Calculator()); if ((i % 2 ) == 0 ) { threads[i].setPriority(Thread.MAX_PRIORITY); } else { threads[i].setPriority(Thread.MIN_PRIORITY); } threads[i].setName( "My Thread " + i); } // Wait for the finalization of the threads. Meanwhile, // write the status of those threads in a file try (FileWriter file = new FileWriter( "src\\data\\log.txt" ); PrintWriter pw = new PrintWriter(file);) { // Write the status of the threads for ( int i = 0 ; i < 10 ; i++) { pw.println( "Main : Status of Thread " + i + " : " + threads[i].getState()); status[i] = threads[i].getState(); } // Start the ten threads for ( int i = 0 ; i < 10 ; i++) { threads[i].start(); } // Wait for the finalization of the threads. We save the status of // the threads and only write the status if it changes. boolean finish = false ; while (!finish) { for ( int i = 0 ; i < 10 ; i++) { if (threads[i].getState() != status[i]) { writeThreadInfo(pw, threads[i], status[i]); status[i] = threads[i].getState(); } } finish = true ; for ( int i = 0 ; i < 10 ; i++) { finish = finish && (threads[i].getState() == State.TERMINATED); } } } catch (IOException e) { e.printStackTrace(); } } /** * This method writes the state of a thread in a file * * @param pw * : PrintWriter to write the data * @param thread * : Thread whose information will be written * @param state * : Old state of the thread */ private static void writeThreadInfo(PrintWriter pw, Thread thread, State state) { pw.printf( "Main : Id %d - %s\n" , thread.getId(), thread.getName()); pw.printf( "Main : Priority: %d\n" , thread.getPriority()); pw.printf( "Main : Old State: %s\n" , state); pw.printf( "Main : New State: %s\n" , thread.getState()); pw.printf( "Main : ************************************\n" ); } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import BLL.CalculatorBLL; public class Main { /** * * @param args */ public static void main(String[] args) { System.out.println( "Hello java language world! 涂聚文!" ); CalculatorBLL cbll= new CalculatorBLL(); cbll.CalculatorExample(); } |
输出:
Concurrency
https://github.com/PacktPublishing/Java-9-Concurrency-Cookbook-Second-Edition
C#
https://github.com/jeremiahflaga/concurrency-in-csharp-cookbook-2e
https://www.oreilly.com/library/view/concurrency-in-c/9781492054498/
python
https://www.oreilly.com/library/view/python-concurrency-with/9781617298660/
https://docs.python.org/3/library/concurrency.html
https://bjpcjp.github.io/pdfs/python/fluent-python-ch18.pdf
https://jython.readthedocs.io/en/latest/Concurrency/
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!