Java multi-thread programming 6: communication between threads (with source code)
Source code download
In multi-threaded programming, communication between threads is a complicated problem. What are competing resources? When should I consider synchronization? How to synchronize? What is thread communication? How to com
. When these options exist, the best practice is to focus instead on using queues. In contrast, queues are easier to handle and can make thread programming more secure because they effectively deliver all the access to a single thread to resources and support clearer, more readable design patterns.
In the next example, you will first create a serial or sequential program that gets the URL for the Web site and displays the first 1024 bytes of the page
The C + + Standard Template Library provides an auxiliary function-std::thread::hardware_concurrency (), through which we can get the number of threads that the application can actually execute concurrently. In this example, a concurrent version of Std::accumulate is implemented, which splits the work into multiple threads, in order to avoid the overhead of excessive th
thread is using CPU resources at a time.Iii. SummaryEach process has its own data space, so the inter-process communication and data sharing will be more complex;Threads share the same runtime environment and share the same piece of data space, and data sharing and inter-thread communication are relatively straightforward.In I/O intensive operation, multi-process and multi-threaded operation efficiency is relatively small;In CPU-intensive operation,
=False, returns immediately without waiting for the task in the pool to complete but regardless of the wait parameter value, the entire program waits until all tasks have been completed submit and map must precede shutdown#result (Timeout=none)Get Results#add_done_callback (FN)callback functionImportOSImport TimeImportRandom fromConcurrent.futuresImportThreadpoolexecutor,processpoolexecutordeffunc (i):Print(i*'*') Time.sleep (1) returnI**2defCall_back (ARG):Print(Arg.result () *'-')if __name_
different I/O. A common way is InputStream's read () method, which blocks until a byte of data is read from the stream, it can block indefinitely, and therefore cannot specify a time-out;4, a thread can also block waiting for exclusive access to an object lock (that is, to wait for a lock that the synchronized statement must have blocked).Note that not all blocking states are interruptible, the first two of the above blocking states can be interrupted, and the latter two will not respond to int
The omission of notify notice is easy to understand, that is, Threada has not started wait time, THREADB has notify, so, THREADB notice is not any response, when threadb exit synchronized code block, Threada starts waiting again and then waits until it is interrupted by another thread.See Example: Https://git.oschina.net/wenjieyatou/threadTestBefore THREADB notifies, the Oktoproceed is set to true so that if Threada misses the notification, it will not enter the while loop, and the wait method w
C++11 is a new standard supported by VS2012, providing a convenient std::thread for concurrent programming.Examples of Use:#include voidThread_func (intArg1,intArg2,float*Arg3) {Arg3= (arg1*1.0)/(Arg1 +arg2); cout"arg1/(arg1 + arg2) ="Endl; return;}voidMain () {//Number of Threads intThreadnum =3 //Dynamic Allocationthread*T; T=NewThread[threadnum]; //Results float*result = (float*)malloc(threadnum*sizeof(float)); for(inti =0; i ) {T[i]= Thr
to the most common problem with multithreaded programming: Data sharing. Synchronization control is required when multiple threads modify a shared data.Thread synchronization ensures that multiple threads secure access to competing resources, and the simplest synchronization mechanism is to introduce mutexes. A mutex introduces a state to a resource: locked/non-
When I was working on a project a few years ago, I referred to the book Programming Server-Side Application For Microsoft Windows 2000, which was written by the famous Jeffery Rechard and the author of Windows core Programming. At that time, the second chapter was translated for others in the Working Group. Today, I have sent this old article to enrich my blog and hope to help others.
Chapter II Device
I/O
Java concurrent Programming: Creating a thread Table of Contents
1. Thread
2. Runnable
3. Start () and run ()
There are two main ways to create threads in Java, one is by inheriting the abstract class thread, and one by implementing the Runnable interface. Of course, there are also concurent bags inside the callable and future can be considered a kind of.1ThreadLet's take a look at how
possibility of deadlocks, such as the problem of philosophers eating. The general solution in the program is to allocate resources to it at once, in order to provide concurrency, we need to further narrow the scope of the concurrent lock.In addition to logically preventing concurrency. We also need to deal with unexpected situations, such as the thread that gets to the resource is dropped halfway. We need to release resources. The lock is released in the program. Can be implemented through Try-
:"+ Thread.getid () +"Thread Name:"+ Thread.getname ()); Pwriter.println ("Thread Priority:"+ thread.getpriority ()); Pwriter.println ("Thread past state:"+ state); Pwriter.println ("Thread Current state:"+ thread.getstate ()); Pwriter.println ("*********************************************************"); }}Partial execution results are as follows:Analysis of the above partial execution results shows that when the pig thread sleeps, it causes the transformation of other thread states, where the
Memory leakage caused by improper use of threads in Linux Programming
Author: Wu Liang
In Linux programming, the pthread_create () function is called when a thread is created. The prototype of this function is as follows:
Int pthread_create (
Pthread_t * thread,
Const pthread_attr_t * ATTR,
Void * (* start_routine) (void *),
Void * Arg );
The second parameter,
Python programming test computer to enable the maximum number of threads instance code, python instance
The example code in this article mainly implements the maximum number of threads enabled by the python programming test computer. The specific implementation code is as follows.
#! /Usr/bin/env python # coding = gbk
above example leads to the most common problem with multithreaded programming: Data sharing. Synchronization control is required when multiple threads modify a shared data.
Thread synchronization ensures that multiple threads secure access to competing resources, and the simplest synchronization mechanism is to introduce mutexes. A mutex introduces a state to a
object.Notify ():Wakes up a thread that waits for the current object's lock, and if multiple threads are waiting, one of them will be chosen to wake up, which is optional.A lock that is awakened cannot be executed and must wait until the current thread discards the lock on the object, and the thread that is awakened and the other threads compete for the lock.There are several ways a thread has a lock on an
the Thread.currentThread(); method.3. Source Code Analysis Public class Thread implements Runnable { //Associated runnable interface PrivateRunnable Target;//Thread logic method Public void Run() {if(Target! =NULL) {Target.run (); } }}//thread interface Public interface Runnable { //define abstract thread logic methods, Public Abstract void Run();}The above thread class and runnable interface are two of the most important core parts of the Java JDK, inheriting that thread
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.