In learning Java, I found that there is less talk about threading, I'm going to give some Java beginners some reference to threading,
Lay the groundwork for an in-depth study of Java. I feature a series of articles on Java threading Programming with the principle of common progress
Java thread Programming (i): Threading Basics
Threads (thread) are in fact abbreviations for controlling threads (thread of control).
Each thread is independent, so that the local variables of each method in the thread are isolated from other threads and are completely private, and therefore
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 gets a separate
A copy of a local variable.
Creating threads from the thread class
public classs test{
public void run(){
for (int i;i<100;i++){
System.out.println("hello_"+i);
}
}
}
will be output hello_0, until the hello_99;
If we call this method in an applet, he will run it in the applet's thread.
impot java.applet.Applet;
public class test extends Applet{
public void init(){
test tt=new test();
tt.run();
}
}
What if we wanted to run the run () method of test with the applet's init ()?
You should make test a subclass of Thread (Java.lang.Thread).
The program is modified to:
public classs test extends Thread{
public void run(){
for (int i;i<100;i++){
System.out.println("hello_"+i);
}
}
}
Next we have to modify the applet:
impot java.applet.Applet;
public class test extends Applet{
public void init(){
test tt=new test();
tt.start();
}
}