- 浏览: 2539118 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Monitor Directories and File(III)Multi Threads
Reading a book named shaojinwensj recently and learn a lot from him.
1. Basic ideas
If we want to start a thread, we need to give it a name.
We need to do something if the thread interrupted
If(Thread.interrupted()){
break;
}
try{
doSomething();
}catch(InterruptedException e){
break;
}
if(Thread.interrupted()){
throw new InterruptedException();
}
ThreadLocal<T>
initialValue
set
get
remove
2. Executors
java.util.concurrent.Executors is the factory class of Executor.
TaskOne.java:
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.concurrent.Callable;
public class TaskOne implements Callable<Object>
{
public Object call() throws Exception
{
System.out.println("TaskOne is called!");
return true;
}
}
TaskTwo.java:
package com.xxxxxx.importdata.filemonitor.multithreads;
public class TaskTwo implements Runnable
{
public void run()
{
System.out.println("TaskTwo is called!");
}
}
SingleExecutor.java:
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class SingleExecutor
{
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException
{
TaskOne taskOne = new TaskOne();
TaskTwo taskTwo = new TaskTwo();
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Object> future1 = executor.submit(taskOne);
future1.get();
Future<?> future2 = executor.submit(taskTwo);
future2.get(3, TimeUnit.SECONDS);
}
}
3. Blocking Queue
blockingQ.put(object) block when queue is full------producer
blockingQ.take() block when queue is empty ---------consumer
ArrayBlockingQueue
LinkedBlockingQueue
SynchronousQueue
Queue<E>
add
offer
remove
poll
element
peek
BlockingQueue<E>
put
take
drainTo(Collection)
we will use put and take, better to use drainTo.
Object object = blockingQ.poll(); //not right
Object object = blockingQ.take(); // right
Object object = blockingQ.poll(1,TimeUnit.SECONDS); // right
A simple BlockingQ implementation
package com.xxxxxx.importdata.filemonitor.multithreads;
import java.util.LinkedList;
import java.util.Queue;
public class BlockingQ
{
private Object notEmpty = new Object();
private Object notFull = new Object();
private Queue<Object> linkedList = new LinkedList<Object>();
private int maxLength = 10;
public Object take() throws InterruptedException
{
synchronized (notEmpty)
{
if (linkedList.size() == 0)
{
notEmpty.wait();
}
synchronized (notFull)
{
if (linkedList.size() == maxLength)
{
notFull.notifyAll();
}
return linkedList.poll();
}
}
}
public void offer(Object object) throws InterruptedException
{
synchronized (notEmpty)
{
if (linkedList.size() == 0)
{
notEmpty.notifyAll();
}
synchronized (notFull)
{
if (linkedList.size() == maxLength)
{
notFull.wait();
}
linkedList.add(object);
}
}
}
}
A lock and condition BlockingQueue
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BlockingQ
{
private Lock lock = new ReentrantLock();
private Condition notEmpty = lock.newCondition();
private Condition notFull = lock.newCondition();
private Queue<Object> linkedList = new LinkedList<Object>();
private int maxLength = 10;
public Object take() throws InterruptedException
{
lock.lock();
try
{
if (linkedList.size() == 0)
{
notEmpty.await();
}
if (linkedList.size() == maxLength)
{
notFull.signalAll();
}
return linkedList.poll();
}
finally
{
lock.unlock();
}
}
public void offer(Object object) throws InterruptedException
{
lock.lock();
try
{
if (linkedList.size() == 0)
{
notEmpty.signalAll();
}
if (linkedList.size() == maxLength)
{
notFull.await();
}
linkedList.add(object);
}
finally
{
lock.unlock();
}
}
}
4. ReentrantLock and Synchronized
Synchronized is a simple Lock, one common Lock will have multi Conditions, but synchronized will be one Lock and
one Condition.
Lock -----> lock(); unlock();
Condition -----> await();signal();signalAll()
synchronized ---> lock();unlock();wait();notify();notifyAll();
Atomic
AtomicInteger
AtomicBoolean
AtomicLong
AtomicReference
references:
CountDownLatch
http://www.iteye.com/topic/1002652
http://www.iteye.com/topic/869109
http://www.iteye.com/topic/581476
CyclicBarrier
http://www.iteye.com/topic/657295
http://www.iteye.com/topic/363625
http://www.iteye.com/topic/713053
Pools
http://w2c2y2.iteye.com/blog/479672
http://www.cnblogs.com/jobs
Reading a book named shaojinwensj recently and learn a lot from him.
1. Basic ideas
If we want to start a thread, we need to give it a name.
We need to do something if the thread interrupted
If(Thread.interrupted()){
break;
}
try{
doSomething();
}catch(InterruptedException e){
break;
}
if(Thread.interrupted()){
throw new InterruptedException();
}
ThreadLocal<T>
initialValue
set
get
remove
2. Executors
java.util.concurrent.Executors is the factory class of Executor.
TaskOne.java:
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.concurrent.Callable;
public class TaskOne implements Callable<Object>
{
public Object call() throws Exception
{
System.out.println("TaskOne is called!");
return true;
}
}
TaskTwo.java:
package com.xxxxxx.importdata.filemonitor.multithreads;
public class TaskTwo implements Runnable
{
public void run()
{
System.out.println("TaskTwo is called!");
}
}
SingleExecutor.java:
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class SingleExecutor
{
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException
{
TaskOne taskOne = new TaskOne();
TaskTwo taskTwo = new TaskTwo();
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Object> future1 = executor.submit(taskOne);
future1.get();
Future<?> future2 = executor.submit(taskTwo);
future2.get(3, TimeUnit.SECONDS);
}
}
3. Blocking Queue
blockingQ.put(object) block when queue is full------producer
blockingQ.take() block when queue is empty ---------consumer
ArrayBlockingQueue
LinkedBlockingQueue
SynchronousQueue
Queue<E>
add
offer
remove
poll
element
peek
BlockingQueue<E>
put
take
drainTo(Collection)
we will use put and take, better to use drainTo.
Object object = blockingQ.poll(); //not right
Object object = blockingQ.take(); // right
Object object = blockingQ.poll(1,TimeUnit.SECONDS); // right
A simple BlockingQ implementation
package com.xxxxxx.importdata.filemonitor.multithreads;
import java.util.LinkedList;
import java.util.Queue;
public class BlockingQ
{
private Object notEmpty = new Object();
private Object notFull = new Object();
private Queue<Object> linkedList = new LinkedList<Object>();
private int maxLength = 10;
public Object take() throws InterruptedException
{
synchronized (notEmpty)
{
if (linkedList.size() == 0)
{
notEmpty.wait();
}
synchronized (notFull)
{
if (linkedList.size() == maxLength)
{
notFull.notifyAll();
}
return linkedList.poll();
}
}
}
public void offer(Object object) throws InterruptedException
{
synchronized (notEmpty)
{
if (linkedList.size() == 0)
{
notEmpty.notifyAll();
}
synchronized (notFull)
{
if (linkedList.size() == maxLength)
{
notFull.wait();
}
linkedList.add(object);
}
}
}
}
A lock and condition BlockingQueue
package com.xxxxx.importdata.filemonitor.multithreads;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BlockingQ
{
private Lock lock = new ReentrantLock();
private Condition notEmpty = lock.newCondition();
private Condition notFull = lock.newCondition();
private Queue<Object> linkedList = new LinkedList<Object>();
private int maxLength = 10;
public Object take() throws InterruptedException
{
lock.lock();
try
{
if (linkedList.size() == 0)
{
notEmpty.await();
}
if (linkedList.size() == maxLength)
{
notFull.signalAll();
}
return linkedList.poll();
}
finally
{
lock.unlock();
}
}
public void offer(Object object) throws InterruptedException
{
lock.lock();
try
{
if (linkedList.size() == 0)
{
notEmpty.signalAll();
}
if (linkedList.size() == maxLength)
{
notFull.await();
}
linkedList.add(object);
}
finally
{
lock.unlock();
}
}
}
4. ReentrantLock and Synchronized
Synchronized is a simple Lock, one common Lock will have multi Conditions, but synchronized will be one Lock and
one Condition.
Lock -----> lock(); unlock();
Condition -----> await();signal();signalAll()
synchronized ---> lock();unlock();wait();notify();notifyAll();
Atomic
AtomicInteger
AtomicBoolean
AtomicLong
AtomicReference
references:
CountDownLatch
http://www.iteye.com/topic/1002652
http://www.iteye.com/topic/869109
http://www.iteye.com/topic/581476
CyclicBarrier
http://www.iteye.com/topic/657295
http://www.iteye.com/topic/363625
http://www.iteye.com/topic/713053
Pools
http://w2c2y2.iteye.com/blog/479672
http://www.cnblogs.com/jobs
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 465NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 361Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 362Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 419Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 462NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 284Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ...
相关推荐
Linux文件系统是操作系统的一个重要组成部分,它是Unix系统的一个演变,为用户提供了一个虚拟存储的抽象,主要包括文件和目录的概念。文件是存储在Linux文件系统中最基本的单位,它是一个字节序列,这些字节可以通过...
Directories - Reading and sorting directories. readdir.c - Reading a directory (readdir). dirsortsize.c - Sort a directory by file size (scandir). dirsortalpha.c - Sort a directory alphabetically ...
报错是因为虚拟机的磁盘用完了,想要正常启动需要进入recovery模式,进去把不需要的文件删除,然后才你那个正常启动。 1.重启Ubuntu,随机长按shift进入grub菜单,或等待grub菜单出现 2.选择Advanced options for ...
- **File Operations**: Creating, moving, copying, and deleting files and directories using commands like `cp`, `mv`, `rm`, and `mkdir`. - **File Permissions**: Understanding and modifying file ...
Material File Picker Material file picker library for Android ... .withFilter(Pattern.compile(".*\.txt$")) // Filtering files and directories by file name using regexp .withFilterDirecto
手册使用了特定的文档约定(Documentation Conventions),并详细讲解了目录结构(Directories and Files)和应用程序代码(Application Code)的组织方式,以及µC-OS-III的板级支持包(Board Support Package, BSP...
本书《LDAP Directories Explained: An Introduction and Analysis》旨在为读者提供一个全面的LDAP入门指南,不仅介绍了LDAP的基本概念和技术细节,还探讨了其在现代信息技术环境中的应用。 #### 二、LDAP与传统...
解决SVN上传提示Empty directoriesis not supported 解决SVN上传提示Empty directoriesis not supported
6 Linux Files, Directories, and Archives 115 Part III Desktop 7 The X Window System, Xorg, and Display Managers 145 8 GNOME 169 9 KDE 197 Part IV Linux Software 10 Software Management 219 11 Offi ce ...
Example - Listing Directories Example - A Storage Allocator Appendix A: Reference Manual Introduction Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions...
Checks the installation of the FrontPage Extensions and verifies that file permissions and FrontPage-specific directories are correct. Problems relating to missing FrontPage-specific directories and ...
UTL_FILE 包的工作机制是这样的:首先要使用 FOPEN 函数,将文件的路径、文件名、以及打开模式的参数传入,然后 Oracle 会到 ALL_DIRECTORIES 视图中查看路径是否已经创建。如果路径和文件名均合法,则该文件被打开...
在使用Microsoft Visual C++ 6.0(简称VC6.0)进行开发时,正确配置directories设置至关重要,因为这直接影响到编译器能否找到必要的头文件、库文件以及可执行文件,从而顺利编译和链接程序。当安装过程中...
Directories, File Systems, and Links 13.NSFileManager - Cocoa and the File System 14.Network Programming With Sockets 15.CFRunLoop 16.kqueue and FSEvents 17.Bonjour 18.Multiprocessing 19.Using NSTask ...
In Linux, everything is treated as a file, which includes hardware devices, directories, and sockets. Each open file is assigned a file descriptor. When a file is closed, its file descriptor is freed ...
Playing with file descriptors and redirection 19 Arrays and associative arrays 25 Visiting aliases 27 Grabbing information about terminal 29 Getting, setting dates, and delays 30 Debugging the script ...
They also cover the Android file system, including import directories and files, so readers can perform basic forensic analysis on file system and SD cards. The book includes access to a wealth of ...
If external file communication is not an issue in a particular Python application, temporary directories and files will be managed by objects in the namespace. With the exception of GamsWorkspace ...