This method will raise a RuntimeError if called more than once on the same thread object. You may override this method in a subclass. Wait until the thread terminates. This blocks the calling thread until the thread whose join method is called terminates — either normally or through an unhandled exception — or until the optional timeout occurs. When the timeout argument is present and not None , it should be a floating point number specifying a timeout for the operation in seconds or fractions thereof.
When the timeout argument is not present or None , the operation will block until the thread terminates. A thread can be join ed many times. It is also an error to join a thread before it has been started and attempts to do so raise the same exception. A string used for identification purposes only.
It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor. The identifier is available even after the thread has exited. This is a non-negative integer, or None if the thread has not been started. This value may be used to uniquely identify this particular thread system-wide until the thread terminates, after which the value may be recycled by the OS.
Similar to Process IDs, Thread IDs are only valid guaranteed unique system-wide from the time the thread is created until the thread has been terminated. This method returns True just before the run method starts until just after the run method terminates. The module function enumerate returns a list of all alive threads.
A boolean value indicating whether this thread is a daemon thread True or not False. This must be set before start is called, otherwise RuntimeError is raised. A primitive lock is a synchronization primitive that is not owned by a particular thread when locked. It is created in the unlocked state. It has two basic methods, acquire and release. When the state is unlocked, acquire changes the state to locked and returns immediately.
When the state is locked, acquire blocks until a call to release in another thread changes it to unlocked, then the acquire call resets it to locked and returns. The release method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised. Locks also support the context management protocol. When more than one thread is blocked in acquire waiting for the state to turn to unlocked, only one thread proceeds when a release call resets the state to unlocked; which one of the waiting threads proceeds is not defined, and may vary across implementations.
The class implementing primitive lock objects. Once a thread has acquired a lock, subsequent attempts to acquire it block, until it is released; any thread may release it. Note that Lock is actually a factory function which returns an instance of the most efficient version of the concrete Lock class that is supported by the platform.
When invoked with the blocking argument set to True the default , block until the lock is unlocked, then set it to locked and return True. When invoked with the blocking argument set to False , do not block. If a call with blocking set to True would block, return False immediately; otherwise, set the lock to locked and return True. When invoked with the floating-point timeout argument set to a positive value, block for at most the number of seconds specified by timeout and as long as the lock cannot be acquired.
A timeout argument of -1 specifies an unbounded wait. It is forbidden to specify a timeout when blocking is false. The return value is True if the lock is acquired successfully, False if not for example if the timeout expired. Release a lock. This can be called from any thread, not only the thread which has acquired the lock.
When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. When invoked on an unlocked lock, a RuntimeError is raised. A reentrant lock is a synchronization primitive that may be acquired multiple times by the same thread.
In the locked state, some thread owns the lock; in the unlocked state, no thread owns it. To lock the lock, a thread calls its acquire method; this returns once the thread owns the lock. To unlock the lock, a thread calls its release method. Reentrant locks also support the context management protocol. This class implements reentrant lock objects. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it.
Note that RLock is actually a factory function which returns an instance of the most efficient version of the concrete RLock class that is supported by the platform.
When invoked without arguments: if this thread already owns the lock, increment the recursion level by one, and return immediately. Otherwise, if another thread owns the lock, block until the lock is unlocked. Once the lock is unlocked not owned by any thread , then grab ownership, set the recursion level to one, and return. If more than one thread is blocked waiting until the lock is unlocked, only one at a time will be able to grab ownership of the lock.
There is no return value in this case. When invoked with the blocking argument set to true, do the same thing as when called without arguments, and return True. When invoked with the blocking argument set to false, do not block.
If a call without an argument would block, return False immediately; otherwise, do the same thing as when called without arguments, and return True. Return True if the lock has been acquired, false if the timeout has elapsed.
Release a lock, decrementing the recursion level. If after the decrement it is zero, reset the lock to unlocked not owned by any thread , and if any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.
If after the decrement the recursion level is still nonzero, the lock remains locked and owned by the calling thread. Only call this method when the calling thread owns the lock. A RuntimeError is raised if this method is called when the lock is unlocked. A condition variable is always associated with some kind of lock; this can be passed in or one will be created by default.
Passing one in is useful when several condition variables must share the same lock. A condition variable obeys the context management protocol : using the with statement acquires the associated lock for the duration of the enclosed block. The acquire and release methods also call the corresponding methods of the associated lock. Other methods must be called with the associated lock held. Once awakened, wait re-acquires the lock and returns.
It is also possible to specify a timeout. The notify method wakes up one of the threads waiting for the condition variable, if any are waiting. For example, the following code is a generic producer-consumer situation with unlimited buffer capacity:. This is inherent to multi-threaded programming. This class implements condition variable objects.
A condition variable allows one or more threads to wait until they are notified by another thread. If the lock argument is given and not None , it must be a Lock or RLock object, and it is used as the underlying lock. Otherwise, a new RLock object is created and used as the underlying lock. Acquire the underlying lock. This method calls the corresponding method on the underlying lock; the return value is whatever that method returns.
Release the underlying lock. This method calls the corresponding method on the underlying lock; there is no return value. Wait until notified or until a timeout occurs. If the calling thread has not acquired the lock when this method is called, a RuntimeError is raised.
Once awakened or timed out, it re-acquires the lock and returns. When the underlying lock is an RLock , it is not released using its release method, since this may not actually unlock the lock when it was acquired multiple times recursively.
Instead, an internal interface of the RLock class is used, which really unlocks it even when it has been recursively acquired several times. NET Core.
If you need to terminate the execution of third-party code forcibly in. NET Core, run it in the separate process and use Process. The System. CancellationToken is not available before. NET Framework 4. To stop a thread in older. NET Framework versions, implement the cooperative cancellation manually using the thread synchronization techniques. For example, you can create the volatile boolean field shouldStop and use it to request the code executed by the thread to stop.
For more information, see volatile in C Reference and System. Use the Thread. Join method to make the calling thread wait for the termination of the thread being stopped. You use the Thread. Sleep method to pause the current thread for a specified amount of time. You can interrupt a blocked thread by calling the Thread. Interrupt method. For more information, see Pausing and interrupting threads. The following table presents some of the Thread properties:.
Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode.
Frequent searches. Other sections. Threading Pages Add to favorites. Catalog excerpts. Open the catalog to page 2. Open the catalog to page 5. Open the catalog to page 7. Open the catalog to page 8. Open the catalog to page 9.
Open the catalog to page CB11 Stack-On 2 Pages. Grinder Tips 16 Pages. Grader Blades 40 Pages. Kennametal Puller Complete Kits 2 Pages. Kennametal Wet Coupling Driver 1 Pages. Underground Mining Pages. Kennametal Metalworking Services Catalog 60 Pages. Tooling Systems News 60 Pages. Oilfield Radial Bearings 2 Pages. Cladding Formulas Oilfield 2 Pages.
General Conveyance Equipment 4 Pages. Twin-Screw 2 Pages. Alloys Brochure Direct 28 Pages. Kennametal Stellite Alloys 15 Pages.
0コメント