- 浏览: 342715 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (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 617原创转载请注明出处:https://agilestyle. ... -
abstract class 和 interface区别
2017-09-06 23:12 326原创转载请注明出处:http://agilestyle.i ... -
Difference between <? super T> and <? extends T>
2017-05-24 10:34 404原创转载请注明出处:http://agilestyle.i ... -
a=a+b和a+=b的区别
2017-03-02 22:47 566原创转载请注明出处:http://agilestyle.i ... -
Java Object 九大方法
2017-02-22 21:46 317原创转载请注明出处:http://agilestyle.i ... -
Java Collections Framework Diagram
2016-12-14 15:43 346原创转载请注明出处:http://agilestyle.i ... -
Java三大领域+Python的技能图谱
2016-08-25 09:48 1724转自:来吧 主流编程语言图谱+知识库都在这了 Ja ... -
Top 8 Diagrams for Understanding Java(转)
2016-08-23 10:24 245转自:http://www.programcreek.co ... -
stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
2016-05-27 14:56 3268原创转载请注明出处:http://agilestyle.i ... -
技术资料链接
2016-03-19 12:59 484LeeCode http://www.cnblogs.c ... -
Java取模
2015-09-13 11:41 661取模的规律:取模的结果符号永远与被除数的符号相同 pack ... -
Java面试——字符串
2013-09-18 22:53 877String中的常用方法 1.写出下面程序的输出结 ... -
Java面试——数组
2013-09-18 22:22 8381.下面哪几个选项是合 ... -
数据类型转换
2013-09-17 22:27 738数据类型的转换方式 ... -
Java面试——数据类型及类型转换
2013-09-16 22:42 12901.int和Integer有什么区别? 2.下面选 ... -
实例成员和类成员的区别
2013-09-16 12:05 1101下面关于实例成员的说法,哪些是正确的?CD A.实例成员也 ... -
clone() 方法
2013-03-11 13:54 737在应用开发过程中,我们可能会需要拷贝(copy)一个现有的 ... -
多线程实现方法
2013-03-06 11:05 676两种实现方法: 继承Thread类, 实现Runna ... -
Polymorphism classic example
2012-07-27 16:01 832The classic example in OOP is t ... -
Java Regular Expressions
2012-07-02 20:51 815使用正则表达式可以方便地对数据进行匹配,还可以 执行更加复杂的 ...
相关推荐
(Example for: Thread, Runnable, Socket, ServerSocket) SampleUsingChecksumClasses.cs: Using checksum classes. (Example for: Adler32, CRC32) SampleUsingCollection.cs: Using collection classes. (Example...
四、线程相关工具类 1、com.baijob.commonTools.thread.BaseRunnable 此类实现了Runnable接口,扩展了功能。 增加名称、ID,调用次数和时间统计、线程停止接口等,并且在线程运行时,不允许此线程第二次启动。 2、...
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.
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 ...
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 ...
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 ...
.\Samples Delphi Win32/Win64 common source for all demos .\Samples\delphi\BroswerDemo Delphi Win32/Win64 Web Browser sample application (all Delphi versions) .\Samples\delphi\BroswerDemo\Resources ...
│ ├─example │ │ A-ProtectConsole.cpp │ │ A-ProtectConsole.h │ │ A-ProtectConsole.sln │ │ A-ProtectConsole.vcxproj │ │ Install.cpp │ │ Install.h │ │ KernelModule.h │ │ ntdll.lib │ ...
Avoiding common pitfalls 133 ■ Listening for services 136 Tracking services 141 4.4 Using services in the paint example 143 Defining a shape service 144 ■ Publishing a shape service 144 Tracking ...
Protocol Usage by Common Internet Applications Section 2.14. Summary Exercises Part 2: Elementary Sockets Chapter 3. Sockets Introduction Section 3.1. Introduction Section 3.2. Socket...
A system is comprised of components and interfaces connected together for a common purpose. Specific topics include microcontrollers, design, verification, hardware/software synchronization, ...
Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...
- Correctly initialize internal common FADT for all 64-bit "X" fields - Fixed a couple table mapping issues during table load - Fixed a couple alignment issues for IA64 - Initialize input array to ...