- 浏览: 362300 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (511)
- AgileMethodology (4)
- RDBMS (32)
- NoSQL (16)
- Java (27)
- Python (28)
- Maven (15)
- Linux (27)
- CommonUtils (13)
- IDE (8)
- JavaScript (17)
- jQuery (2)
- OSGi (2)
- JavaWeb (5)
- Spring (37)
- Struts2 (3)
- ORM (13)
- Ant (1)
- apache-tiles (1)
- FreeMarker (2)
- JSON (8)
- XML (1)
- JUnit (3)
- Lucene (1)
- Web Service (9)
- Design Pattern (13)
- Algorithm (21)
- JVM (14)
- Hadoop (16)
- Spark (5)
- Scala (31)
- Git (4)
- Server (3)
- Node.js (18)
- Concurrent (42)
- Lock (9)
- Collections (3)
- Network (11)
- MicroService (7)
- Docker (13)
- FP (20)
- spring.io (2)
- ELK (1)
- Kafka (5)
最新评论
1.设计 4 个线程,其中两个线程每次对 j增加1,另外两个线程对j每次减少1。
public class ThreadTest { public static void main(String[] args) { MyThread thread = new MyThread(); for (int i = 0; i < 2; i++) { Thread inc = new Thread(new Inc(thread)); Thread dec = new Thread(new Dec(thread)); inc.start(); dec.start(); } } } class MyThread { private int j; public synchronized void dec() { j--; System.out.println(Thread.currentThread().getName() + "-dec:" + j); } public synchronized void inc() { j++; System.out.println(Thread.currentThread().getName() + "-inc:" + j); } } class Dec implements Runnable { private MyThread obj; public Dec(MyThread obj) { this.obj = obj; } @Override public void run() { this.obj.dec(); } } class Inc implements Runnable { private MyThread obj; public Inc(MyThread obj) { this.obj = obj; } @Override public void run() { this.obj.inc(); } }
2.设计两个线程,第一个线程从1加到10,在这加的过程中,第二个线程必须等待,当第一个线程加到10时,第二个线程启动,从10减到1,在这减的过程中,第一个线程必须等待,循环执行若干时间后,退出程序。
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class ThreadTest { public static void main(String[] args) throws Exception { final Num num = new Num(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new Runnable() { @Override public void run() { try { while (!Thread.interrupted()) { TimeUnit.MILLISECONDS.sleep(200); num.increased(); num.waitForDecreasing(); } } catch (InterruptedException e) { System.out.println("Exiting via interrupt"); } System.out.println("Ending Increase"); } }); exec.execute(new Runnable() { @Override public void run() { try { while (!Thread.interrupted()) { num.waitForIncreasing(); TimeUnit.MILLISECONDS.sleep(200); num.decreased(); } } catch (InterruptedException e) { System.out.println("Exiting via interrupt"); } System.out.println("Ending Decrease"); } }); TimeUnit.SECONDS.sleep(5); // Run for a while... exec.shutdownNow(); // Interrupt all tasks } } class Num { private boolean flag = false; private int i; public synchronized void increased() { flag = true; for (i = 1; i < 11; i++) { System.out.println(Thread.currentThread().getName() + "-inc " + i); } notifyAll(); } public synchronized void decreased() { flag = false; for (i = 10; i > 0; i--) { System.out.println(Thread.currentThread().getName() + "-dec " + i); } notifyAll(); } public synchronized void waitForIncreasing() throws InterruptedException { while (flag == false) wait(); } public synchronized void waitForDecreasing() throws InterruptedException { while (flag == true) wait(); } }
发表评论
-
Java 8 获取当前日期
2018-12-12 14:38 653原创转载请注明出处:https://agilestyle. ... -
abstract class 和 interface区别
2017-09-06 23:12 364原创转载请注明出处:http://agilestyle.i ... -
Difference between <? super T> and <? extends T>
2017-05-24 10:34 448原创转载请注明出处:http://agilestyle.i ... -
a=a+b和a+=b的区别
2017-03-02 22:47 616原创转载请注明出处:http://agilestyle.i ... -
Java Object 九大方法
2017-02-22 21:46 354原创转载请注明出处:http://agilestyle.i ... -
Java Collections Framework Diagram
2016-12-14 15:43 383原创转载请注明出处:http://agilestyle.i ... -
Java三大领域+Python的技能图谱
2016-08-25 09:48 1768转自:来吧 主流编程语言图谱+知识库都在这了 Ja ... -
Top 8 Diagrams for Understanding Java(转)
2016-08-23 10:24 268转自:http://www.programcreek.co ... -
stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
2016-05-27 14:56 3355原创转载请注明出处:http://agilestyle.i ... -
技术资料链接
2016-03-19 12:59 556LeeCode http://www.cnblogs.c ... -
Java取模
2015-09-13 11:41 704取模的规律:取模的结果符号永远与被除数的符号相同 pack ... -
Java面试——字符串
2013-09-18 22:53 917String中的常用方法 1.写出下面程序的输出结 ... -
Java面试——数组
2013-09-18 22:22 8951.下面哪几个选项是合 ... -
数据类型转换
2013-09-17 22:27 780数据类型的转换方式 ... -
Java面试——数据类型及类型转换
2013-09-16 22:42 13521.int和Integer有什么区别? 2.下面选 ... -
实例成员和类成员的区别
2013-09-16 12:05 1135下面关于实例成员的说法,哪些是正确的?CD A.实例成员也 ... -
clone() 方法
2013-03-11 13:54 768在应用开发过程中,我们可能会需要拷贝(copy)一个现有的 ... -
多线程实现方法
2013-03-06 11:05 701两种实现方法: 继承Thread类, 实现Runna ... -
Polymorphism classic example
2012-07-27 16:01 864The classic example in OOP is t ... -
Java Regular Expressions
2012-07-02 20:51 847使用正则表达式可以方便地对数据进行匹配,还可以 执行更加复杂的 ...
相关推荐
- Libraries such as cuBLAS, cuFFT, and cuSPARSE provide optimized implementations of common algorithms. 9. **Multi-GPU and Distributed Computing** - CUDA supports multi-GPU configurations and ...
(Example for: Thread, Runnable, Socket, ServerSocket) SampleUsingChecksumClasses.cs: Using checksum classes. (Example for: Adler32, CRC32) SampleUsingCollection.cs: Using collection classes. (Example...
在这个修复包中,"common"文件夹可能包含了通用的CUDA库函数或工具,而"julia"文件夹则可能包含针对Julia迭代算子的修复或优化代码。Julia迭代是一种常见的科学计算和图像处理算法,它在复平面上进行迭代,用于生成...
四、线程相关工具类 1、com.baijob.commonTools.thread.BaseRunnable 此类实现了Runnable接口,扩展了功能。 增加名称、ID,调用次数和时间统计、线程停止接口等,并且在线程运行时,不允许此线程第二次启动。 2、...
CUDA编程的核心在于理解和运用CUDA的执行模型,包括线程块(thread blocks)、线程(threads)和网格(grids)的概念。开发者需要知道如何有效地组织这些线程单元,以便最大限度地利用GPU的并行性。此外,CUDA编程还...
- **7.1–1 Breaking Strength of Thread**: This section focuses on the statistical analysis of thread breaking strength, a common problem in textile engineering. It demonstrates the use of statistical ...
Configuring Some Common Web Servers O'Reilly's WebSite H NCSA httpd H Apache H G Examining Some Other File- and Site-Administration Issues G From Here G Chapter 11 Database Interaction ...
Version: 2.14.0 (2020-12-18) Keil.STM32F7xx_DFP.2.14.0.pack STM32CubeMX integration: Added support for USB PHY configuration (MX_...Reduced Idle and Timer thread stack size. Reworked README.md format.
Version: 2.14.0 (2020-12-18) Keil.STM32F7xx_DFP.2.14.0.pack Download STM32CubeMX integration: Added support for USB PHY ...Reduced Idle and Timer thread stack size. Reworked README.md format.
在使用Canal监听MySQL数据库时,可能会遇到各种配置问题,导致Canal无法正常捕获数据库中的变更事件。这里我们分析并解决一个特定的异常情况,该情况发生在尝试监听数据库操作时,Canal无法获取到主库的状态信息。...
10. Thread 11. File 12. Generics 13. I18N 14. Swing 15. Swing Event 16. 2D Graphics 17. SWT 18. SWT 2D Graphics 19. Network 20. Database 21. Hibernate 22. JPA 23. JSP 24. JSTL 25. ...
Further, you'll learn about server scalability, asynchronous I/O, and thread pools, and write responsive traditional Windows and Windows Store applications. By the end of the book, you will be able ...
#include<pcl/common/common_headers.h> #include #include #include #include ``` 这里包含了必要的头文件,用于支持 I/O 操作、数据结构定义、可视化工具等。 ##### 2. 帮助函数 ```cpp void printUsage(const ...
Further, you'll learn about server scalability, asynchronous I/O, and thread pools, and write responsive traditional Windows and Windows Store applications. By the end of the book, you will be able ...
**Note**: This question is not directly related to technical knowledge but rather a common interview question. For the purpose of this document, we will focus on technical questions. #### 5. Please ...
它主要包括了CLDC(Common Language Device Configuration)和CDC(Connected Device Configuration)两个配置,以及一系列用于不同设备的Profile。在这个案例中使用的主要是MIDP(Mobile Information Device Profile),...
Thread Priorities 448 Synchronization 449 Summary 453 Chapter 16: Distributed Applications with .NET Remoting 455 What Is .NET Remoting? 456 Application Types and Protocols 456 CLR Object Remoting 457...
Common Stumbling Blocks Print Is A Function Views And Iterators Instead Of Lists Ordering Comparisons Integers Text Vs. Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax ...
Common Misconceptions about Java 11 Chapter 2: The Java Programming Environment 15 Installing the Java Development Kit 16 Choosing a Development Environment 21 Using the Command-Line Tools 22 ...
This lesson outlines some of the common causes that contribute to the perception of a slow server. What You Will Learn After completing this lesson, you will be able to: Describe locking ...