Overrides: clone in class Object Returns: a clone of this instance. The result is that two threads are running concurrently: the current thread which returns from the call to the start method and the other thread which executes its run method. It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution. Throws: IllegalThreadStateException - if the thread was already started.
See Also: run , stop run public void run If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method. Forces the thread to stop executing. If there is a security manager installed, its checkAccess method is called with this as its argument.
This may result in a SecurityException being raised in the current thread. If this thread is different from the current thread that is, the current thread is trying to stop a thread other than itself , the security manager's checkPermission method with a RuntimePermission "stopThread" argument is called in addition.
Again, this may result in throwing a SecurityException in the current thread. The thread represented by this thread is forced to stop whatever it is doing abnormally and to throw a newly created ThreadDeath object as an exception. It is permitted to stop a thread that has not yet been started.
If the thread is eventually started, it immediately terminates. An application should not normally try to catch ThreadDeath unless it must do some extraordinary cleanup operation note that the throwing of ThreadDeath causes finally clauses of try statements to be executed before the thread officially dies. If a catch clause catches a ThreadDeath object, it is important to rethrow the object so that the thread actually dies. The top-level error handler that reacts to otherwise uncaught exceptions does not print out a message or otherwise notify the application if the uncaught exception is an instance of ThreadDeath.
Throws: SecurityException - if the current thread cannot modify this thread. Permission stop Deprecated public final void stop Throwable obj Deprecated. Throws UnsupportedOperationException. Parameters: obj - ignored interrupt public void interrupt Interrupts this thread. Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait , wait long , or wait long, int methods of the Object class, or of the join , join long , join long, int , sleep long , or sleep long, int , methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set. Interrupting a thread that is not alive need not have any effect. Throws: SecurityException - if the current thread cannot modify this thread interrupted public static boolean interrupted Tests whether the current thread has been interrupted.
The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it. A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.
Returns: true if the current thread has been interrupted; false otherwise. See Also: isInterrupted isInterrupted public boolean isInterrupted Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method. Returns: true if this thread has been interrupted; false otherwise. See Also: interrupted destroy Deprecated public void destroy Deprecated.
Throws NoSuchMethodError. A thread is alive if it has been started and has not yet died. Returns: true if this thread is alive; false otherwise. Suspends this thread. First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException in the current thread. If the thread is alive, it is suspended and makes no further progress unless and until it is resumed.
See Also: checkAccess resume Deprecated public final void resume Deprecated. Resumes a suspended thread. If the thread is alive but suspended, it is resumed and is permitted to make progress in its execution.
See Also: checkAccess , suspend setPriority public final void setPriority int newPriority Changes the priority of this thread. First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException. Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group. SecurityException - if the current thread cannot modify this thread.
Returns: this thread's priority. See Also: setPriority int setName public final void setName String name Changes the name of this thread to be equal to the argument name. Parameters: name - the new name for this thread.
Returns: this thread's name. This method returns null if this thread has died been stopped. Returns: this thread's thread group. Recursively iterates over all subgroups in the current thread's thread group. The value returned is only an estimate because the number of threads may change dynamically while this method traverses internal data structures, and might be affected by the presence of certain system threads.
This method is intended primarily for debugging and monitoring purposes. Returns: an estimate of the number of active threads in the current thread's thread group and in any other thread group that has the current thread's thread group as an ancestor enumerate public static int enumerate Thread [] tarray Copies into the specified array every active thread in the current thread's thread group and its subgroups.
This method simply invokes the ThreadGroup. An application might use the activeCount method to get an estimate of how big the array should be, however if the array is too short to hold all the threads, the extra threads are silently ignored. If it is critical to obtain every active thread in the current thread's thread group and its subgroups, the invoker should verify that the returned int value is strictly less than the length of tarray.
Due to the inherent race condition in this method, it is recommended that the method only be used for debugging and monitoring purposes. Parameters: tarray - an array into which to put the list of threads Returns: the number of threads put into the array Throws: SecurityException - if ThreadGroup. Counts the number of stack frames in this thread. The thread must be suspended. Returns: the number of stack frames in this thread.
Throws: IllegalThreadStateException - if this thread is not suspended. A timeout of 0 means to wait forever. This implementation uses a loop of this. As a thread terminates the this.
It is recommended that applications not use wait , notify , or notifyAll on Thread instances. Parameters: millis - the time to wait in milliseconds Throws: IllegalArgumentException - if the value of millis is negative InterruptedException - if any thread has interrupted the current thread.
Parameters: millis - the time to wait in milliseconds nanos - additional nanoseconds to wait Throws: IllegalArgumentException - if the value of millis is negative, or the value of nanos is not in the range InterruptedException - if any thread has interrupted the current thread. An invocation of this method behaves in exactly the same way as the invocation join 0 Throws: InterruptedException - if any thread has interrupted the current thread.
This method is used only for debugging. See Also: Throwable. The Java Virtual Machine exits when the only threads running are all daemon threads. This method must be invoked before the thread is started. Parameters: on - if true , marks this thread as a daemon thread Throws: IllegalThreadStateException - if this thread is alive SecurityException - if checkAccess determines that the current thread cannot modify this thread isDaemon public final boolean isDaemon Tests if this thread is a daemon thread.
Returns: true if this thread is a daemon thread; false otherwise. See Also: setDaemon boolean checkAccess public final void checkAccess Determines if the currently running thread has permission to modify this thread. If there is a security manager, its checkAccess method is called with this thread as its argument. Throws: SecurityException - if the current thread is not allowed to access this thread. See Also: SecurityManager.
Overrides: toString in class Object Returns: a string representation of this thread. The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not set , the default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application.
If a security manager is present, and the invoker's class loader is not null and is not the same as or an ancestor of the context class loader, then this method invokes the security manager's checkPermission method with a RuntimePermission "getClassLoader" permission to verify that retrieval of the context class loader is permitted. Returns: the context ClassLoader for this Thread, or null indicating the system class loader or, failing that, the bootstrap class loader Throws: SecurityException - if the current thread cannot get the context ClassLoader Since: 1.
The context ClassLoader can be set when a thread is created, and allows the creator of the thread to provide the appropriate class loader, through getContextClassLoader , to code running in the thread when loading classes and resources. If a security manager is present, its checkPermission method is invoked with a RuntimePermission "setContextClassLoader" permission to see if setting the context ClassLoader is permitted. Parameters: cl - the context ClassLoader for this Thread, or null indicating the system class loader or, failing that, the bootstrap class loader Throws: SecurityException - if the current thread cannot set the context ClassLoader Since: 1.
Report Error. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use , cookie and privacy policy.
Copyright by Refsnes Data. All Rights Reserved. This means our costly hardware will remain idle for most of the time. Using multiple threads, we can take full advantage of multiple cores by serving more clients and serving them faster.
Multi-threading is one way to exploiting the vast computing power of the CPU in Java application. Even Java application contains at least one thread called the main thread, which executes your main method.
There are more threads used by JVM, e. As application developer, we can also add new user threads to make our application faster and more efficient. Here are a couple of common reasons and scenarios to use multiple threads in Java.
One of the main reasons to use threads in Java is to make a task run parallel to another task e. GUI applications e. In a typical GUI application, the user initiates an action e. These actions require some time to complete but we cannot freeze the GUI because then the user will think our application is hung.
Instead, we need a separate thread to carry out the time-consuming task and keep showing relevant messages to the user or allow him to do other tasks at the same time to keep your GUI alive. This is achieved by using multiple threads in Java. Another common reason for using multiple threads in Java is to improve the throughput of the application by utilizing full CPU power. For example, if we have got 32 core CPU and we are only using 1 of them for serving clients and assuming that our application is CPU bound, we can improve throughput to 32 times by using 32 threads, which will utilize all 32 cores of your CPU.
We can also use multiple threads to reduce response time by doing fast computation by dividing a big problem into smaller chunks and processing them by using multiple threads. For example, the map-reduce pattern is based upon dividing a big problem into smaller ones and processing them. A single-threaded application means only one client can connect to the server at a time, but a multi-threaded server means multiple clients can connect to the server at the same time. There are two ways to create a thread:.
Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements the Runnable interface. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.
0コメント