一 基本概念
多任务:同一时刻运行多个程序的能力。每一个任务称为一个线程。可以同时运行一个以上线程的程序称为多线程程序。
Java编写程序都运行在在Java虚拟机(JVM)中,在JVM的内部,程序的多任务是通过线程来实现的。每用java命令启动一个java应用程序,就会启动一个JVM进程。在同一个JVM进程中,有且只有一个进程,就是它自己。在这个JVM环境中,所有程序代码的运行都是以线程来运行。
一般常见的Java应用程序都是单线程的。比如,用java命令运行一个最简单的HelloWorld的Java应用程序时,就启动了一个JVM进程,JVM找到程序程序的入口点main(),然后运行main()方法,这样就产生了一个线程,这个线程称之为主线程。当main方法结束后,主线程运行完成。JVM进程也随即退出 。
对于一个进程中的多个线程来说,多个线程共享进程的内存块,当有新的线程产生的时候,操作系统不分配新的内存,而是让新线程共享原有的进程块的内存。因此,线程间的通信很容易,速度也很快。不同的进程因为处于不同的内存块,因此进程之间的通信相对困难。
进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程。比如在Windows系统中,一个运行的exe就是一个进程。
线程是指进程中的一个执行流程,一个进程可以运行多个线程。比如java.exe进程可以运行很多线程。线程总是输入某个进程,进程中的多个线程共享进程的内存。
1 package Thread; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 public class BounceThread { 6 public static void main(String[] args){ 7 EventQueue.invokeLater(new Runnable(){ 8 public void run(){ 9 JFrame frame=new BounceFrame(); 10 frame.setTitle("BounceFrame"); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 frame.setVisible(true); 13 } 14 }); 15 } 16 } 17 /*class BallRunnable implements Runnable{ 18 private Ball ball; 19 private Component component; 20 public static final int STEPS=1000; 21 public static final int DELAY=5; 22 public BallRunnable(Ball aBall,Component aComponent){ 23 ball=aBall; 24 component=aComponent; 25 } 26 public void run(){ 27 try{ 28 for(int i=1;i<=STEPS;i++){ 29 ball.move(component.getBounds()); 30 component.repaint(); 31 Thread.sleep(DELAY); 32 } 33 } 34 catch(InterruptedException e){} 35 } 36 }*/ 37 class BounceFrame extends JFrame{ 38 private BallComponent comp; 39 public static final int STEPS=1000; 40 public static final int DELAY=100; 41 public BounceFrame(){ 42 comp=new BallComponent(); 43 add(comp,BorderLayout.CENTER); 44 JPanel buttonPanel=new JPanel(); 45 addButton(buttonPanel,"Start",new ActionListener(){ 46 public void actionPerformed(ActionEvent event){ 47 addBall(); 48 } 49 }); 50 addButton(buttonPanel,"Close",new ActionListener(){ 51 public void actionPerformed(ActionEvent event){ 52 System.exit(0); 53 } 54 }); 55 add(buttonPanel,BorderLayout.SOUTH); 56 pack(); 57 } 58 public void addButton(Container c,String title,ActionListener listener){ 59 JButton button=new JButton(title); 60 c.add(button); 61 button.addActionListener(listener); 62 } 63 /*public void addBall(){ 64 Ball b=new Ball(); 65 comp.add(b); 66 Runnable r=new BallRunnable(b,comp); 67 Thread t=new Thread(r); 68 t.start(); 69 }*/ 70 public void addBall(){ 71 try{ 72 Ball ball=new Ball(); 73 comp.add(ball); 74 for(int i=1;i<=STEPS;i++){ 75 ball.move(comp.getBounds()); 76 comp.paint(comp.getGraphics()); 77 Thread.sleep(DELAY); 78 } 79 } 80 catch(InterruptedException e){} 81 } 82 }
BollComponent.java
1 package Thread; 2 import java.awt.*; 3 4 import java.util.*; 5 import javax.swing.*; 6 public class BallComponent extends JPanel{ 7 private static final int DEFAULT_WIDTH=450; 8 private static final int DEFAULT_HEIGHT=350; 9 private java.util.List<Ball>balls=new ArrayList<>(); 10 public void add(Ball b){ 11 balls.add(b); 12 } 13 public void paintComponent(Graphics g){ 14 super.paintComponent(g); 15 Graphics2D g2=(Graphics2D)g; 16 for(Ball b:balls){ 17 g2.fill(b.getShape()); 18 } 19 } 20 public Dimension getPreferredSize(){ 21 return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT); 22 } 23 }
Ball.java
1 package Thread; 2 import java.awt.geom.*; 3 import java.awt.geom.Ellipse2D.Double; 4 public class Ball { 5 private static final int XSIZE=15; 6 private static final int YSIZE=15; 7 private double x=0; 8 private double y=0; 9 private double dx=1; 10 private double dy=1; 11 public void move(Rectangle2D bounds){ 12 x+=dx; 13 y+=dy; 14 if(x<bounds.getMinX()){ 15 x=bounds.getMinX(); 16 dx=-dx; 17 } 18 if(x+XSIZE>=bounds.getMaxX()){ 19 x=bounds.getMaxX()-XSIZE; 20 dx=-dx; 21 } 22 if(y<bounds.getMinY()){ 23 y=bounds.getMinY(); 24 dy=-dy; 25 } 26 if(y+YSIZE>=bounds.getMaxY()){ 27 y=bounds.getMaxY()-YSIZE; 28 dy=-dy; 29 } 30 } 31 public Ellipse2D getShape(){ 32 return new Ellipse2D.Double(x,y,XSIZE,YSIZE); 33 } 34 }
针对上述的情况,下面的代码是改进后的,当点击close时,就会退出当前线程。而且不论何时点击Start按钮,addBall都会启动一个新线程.
实现多个线程的方法:将移动球的代码放置在一个独立的线程中,点击开始就会重新启动一个线程。简单过程如下:
1、将任务代码放在实现了Runnable接口的类的run方法中。
1 class MyRunnable implements Runnable{ 2 public void run(){ 3 task code 4 } 5 }
2、创建一个类对象。Runnable r=new MyRunnable();
3、由Runnable创建一个Thread对象。Thread t=new Thread();
4、启动线程:t.start();
BounceThread.java
1 package Thread; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 public class BounceThread { 6 public static void main(String[] args){ 7 EventQueue.invokeLater(new Runnable(){ 8 public void run(){ 9 JFrame frame=new BounceFrame(); 10 frame.setTitle("BounceFrame"); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 frame.setVisible(true); 13 } 14 }); 15 } 16 } 17 class BallRunnable implements Runnable{ 18 private Ball ball; 19 private Component component; 20 public static final int STEPS=1000; 21 public static final int DELAY=5; 22 public BallRunnable(Ball aBall,Component aComponent){ 23 ball=aBall; 24 component=aComponent; 25 } 26 public void run(){ 27 try{ 28 for(int i=1;i<=STEPS;i++){ 29 ball.move(component.getBounds()); 30 component.repaint(); 31 Thread.sleep(DELAY); 32 } 33 } 34 catch(InterruptedException e){} 35 } 36 } 37 class BounceFrame extends JFrame{ 38 private BallComponent comp; 39 //public static final int STEPS=1000; 40 //public static final int DELAY=100; 41 public BounceFrame(){ 42 comp=new BallComponent(); 43 add(comp,BorderLayout.CENTER); 44 JPanel buttonPanel=new JPanel(); 45 addButton(buttonPanel,"Start",new ActionListener(){ 46 public void actionPerformed(ActionEvent event){ 47 addBall(); 48 } 49 }); 50 addButton(buttonPanel,"Close",new ActionListener(){ 51 public void actionPerformed(ActionEvent event){ 52 System.exit(0); 53 } 54 }); 55 add(buttonPanel,BorderLayout.SOUTH); 56 pack(); 57 } 58 public void addButton(Container c,String title,ActionListener listener){ 59 JButton button=new JButton(title); 60 c.add(button); 61 button.addActionListener(listener); 62 } 63 public void addBall(){ 64 Ball b=new Ball(); 65 comp.add(b); 66 Runnable r=new BallRunnable(b,comp); 67 Thread t=new Thread(r); 68 t.start(); 69 } 70 /*public void addBall(){ 71 try{ 72 Ball ball=new Ball(); 73 comp.add(ball); 74 for(int i=1;i<=STEPS;i++){ 75 ball.move(comp.getBounds()); 76 comp.paint(comp.getGraphics()); 77 Thread.sleep(DELAY); 78 } 79 } 80 catch(InterruptedException e){} 81 }*/ 82 }
BollComponent.java
1 package Thread; 2 import java.awt.*; 3 4 import java.util.*; 5 import javax.swing.*; 6 public class BallComponent extends JPanel{ 7 private static final int DEFAULT_WIDTH=450; 8 private static final int DEFAULT_HEIGHT=350; 9 private java.util.List<Ball>balls=new ArrayList<>(); 10 public void add(Ball b){ 11 balls.add(b); 12 } 13 public void paintComponent(Graphics g){ 14 super.paintComponent(g); 15 Graphics2D g2=(Graphics2D)g; 16 for(Ball b:balls){ 17 g2.fill(b.getShape()); 18 } 19 } 20 public Dimension getPreferredSize(){ 21 return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT); 22 } 23 }
Ball.java
1 package Thread; 2 import java.awt.geom.*; 3 import java.awt.geom.Ellipse2D.Double; 4 public class Ball { 5 private static final int XSIZE=15; 6 private static final int YSIZE=15; 7 private double x=0; 8 private double y=0; 9 private double dx=1; 10 private double dy=1; 11 public void move(Rectangle2D bounds){ 12 x+=dx; 13 y+=dy; 14 if(x<bounds.getMinX()){ 15 x=bounds.getMinX(); 16 dx=-dx; 17 } 18 if(x+XSIZE>=bounds.getMaxX()){ 19 x=bounds.getMaxX()-XSIZE; 20 dx=-dx; 21 } 22 if(y<bounds.getMinY()){ 23 y=bounds.getMinY(); 24 dy=-dy; 25 } 26 if(y+YSIZE>=bounds.getMaxY()){ 27 y=bounds.getMaxY()-YSIZE; 28 dy=-dy; 29 } 30 } 31 public Ellipse2D getShape(){ 32 return new Ellipse2D.Double(x,y,XSIZE,YSIZE); 33 } 34 }
运行结果如下: