Java Thread Programming (1): thread Basics

Source: Internet
Author: User
Java Thread Programming (1): thread basics-Linux general technology-Linux programming and kernel information. For details, refer to the following section. In learning java, I found that there are few explanations on threads. I plan to provide some thread-related references for some java beginners,
Lay the foundation for deep learning java. I wrote a series of articles on java thread programming based on the principle of common progress.

Java Thread Programming (1): thread Basics

◆ Threads are short for controlling threads.
Each thread is independent. Therefore, the local variables of each method in the thread are isolated from other threads. These variables are completely private. Therefore,
For threads, there is no way to access the local variables of other threads. If two threads access the same method at the same time, each thread obtains a separate
Copy local variables.

◆ Create a Thread through the Thread class

Public classs test {
Public void run (){
For (int I; I <100; I ++ ){
System. out. println ("hello _" + I );
}
}
}

Hello_0 will be output until hello_99;
If we call this method in the Applet, it will run in the Applet thread.
Impot java. applet. Applet;

Public class test extends Applet {
Public void init (){
Test tt = new test ();
Tt. run ();
}
}


If we want the run () method of test to run together with the init () method of applet, what should we do?
Make test a subclass of thread (java. lang. Thread.
Program modified:
Public classs test extends Thread {
Public void run (){
For (int I; I <100; I ++ ){
System. out. println ("hello _" + I );
}
}
}
Next we need to modify the Applet:
Impot java. applet. Applet;

Public class test extends Applet {
Public void init (){
Test tt = new test ();
Tt. start ();

}
}
The call to the ran () method is changed to the call to the start () method.

We will provide some instructions:
Thread
Create a process object
Void run ()
The newly created thread executes this method.
Void start ()
Create a new thread and run the run () method defined in this thread class.

Let's take an example.

Import java. awt .*;

Public class TimerThread extends Thread {
Componet comp; // component to be repainted
Int timediff; // The time interval between the re-painting of the component.
Volatile boolean shouldrun; // set false to stop a thread.


Public TimerThread (Compond comp, int timediff ){
This. comp = comp;
This. timediff = timedifff;
Shouldrun = true;
}


Public void run (){
While (shouldrun ){
Try {
Comp. repaint ();
Sleep (timediff );
} Catch (Exception e ){}

}

}
}
The sleep () method is part of the Thread class, which causes the current Thread to stop for a certain period of time.
Static void sleep (long millseconds)
Set the sleep time to milliseconds.
Static void sleep (long millseconds, int nanoseconds)
The method of specifying the sleep time as millisecond and second is static.


Thread stop, generally not recommended
The stop () method is dangerous and will cause exceptions.



◆ Threads using the Runable Interface
Java uses an interface mechanism to solve the multi-inheritance that it does not support.
The Runnable interface only uses one method run.

Public classs test implements Runnable {
Public void run (){
For (int I; I <100; I ++ ){
System. out. println ("hello _" + I );
}
}
}

At the same time, the corresponding program is modified:
Impot java. applet. Applet;

Public class test extends Applet {
Public void init (){
Runnable tt = new test ();
Thread th = new Thread (tt );
Th. start ()'

}
}

◆ Thread cycle

Boolean isAlive ()
Used to determine whether a thread is active or not
Starting a thread does not mean that the thread is running or can run. This is the reason.
IsAlive () is often used to detect whether a thread is running.


A simple small program

...
While (timer. isAlive ())
{
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){}
}

Void jion ()
Wait until the specified Thread finishes running
When the thread is not active, jion () will return

Void jion (long timeout)
Wait until the specified Thread finishes running, but cannot exceed the specified timeout value

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.