Java thread Programming (i): Threading Basics

Source: Internet
Author: User
Tags thread thread class

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();
}
}

Related Article

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.