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