Thread of Java

Source: Internet
Author: User

1. threading is a single sequential control process in a program. A relatively independent, scheduled execution unit within a process is the unit of dispatch for the system to dispatch and dispatch CPUs independently of the operating program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading.

2. What is multithreading?

Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading.

Implementation of 3.Java Threads
One is to implement the Runnable interface
One is to inherit the thread class

Using threads to control the movement of plane.jpg pictures

ImportJavax.swing.JFrame; Public classPlanedemoextendsJFrameImplementsRunnable { Public Static voidMain (string[] args) {planedemo pd=NewPlanedemo (); Thread T=NewThread (PD);    T.start (); }     PublicPlanedemo () {Initui (); }    Private voidInitui () {image=NewImageIcon ( This. GetClass (). GetResource ("Plane.jpg") . GetImage ();  This. Settitle ("Thread Control picture movement");  This. SetSize (600, 400);  This. Setlocationrelativeto (NULL);  This. setdefaultcloseoperation (3);  This. setresizable (false);  This. setvisible (true); }    Privateimage Image; Private inty = 300; /*** Override the form's Paint method*/     Public voidPaint (Graphics g) {Super. Paint (g); G.drawimage (Image,, y, 100, 100, This); }    /*** Override the Run method of the Runnable interface*/     Public voidrun () { while(true) {y-=3; if(y<=50) y= 300;            Repaint (); Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }

Thread of Java

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.