进程:一个程序的执行 一个进程可以有多个线程
线程:程序中单个顺序的流程控制
多线程在底层实际上是将CPU时间片化,在宏观上像同时发生而已
Java从语言级别支持多线程 java.lang中的类Thread
线程体,run()来实现 线程启动(start)后,系统自动调用run()方法
通常 run方法执行一个时间较长的操作 如一个循环 显示一系列图片 下载一个文件等
两种方法创建线程
1 class MyThread extends Thread{//继承类 重写run方法
public void run(){
}
}
2 class MyThread implements Runnable{ //接口 本质上是一个run的方法
public void run(){
}
}
例子
import java.util.*;
import java.util.Calendar;
class testThread3{
public static void main(String args[]) {
Counter c1=new Counter(1);//runnable方法
Thread t1=new Thread(c1);//创建了3个 c1的线程
Thread t2=new Thread(c1);
Thread t3=new Thread(c1);
Counter c2=new Counter(2);//runnable方法
Thread t4=new Thread(c2);//创建了4个c2的方法
Thread t5=new Thread(c2);
Thread t6=new Thread(c2);
Thread t7=new Thread(c2);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
}
}
class Counter implements Runnable{
int id;
Counter(int id){
this.id=id;
}
public void run() {//线程跑起来
int i=0;
while(i++<=2) {
System.out.println("ID: "+id+" No. "+i);
try{Thread.sleep(10);}catch(InterruptedException e) {}
}
}
}
运行结果
ID: 1 No. 1 怎么里理解 看输出 i=1的时No.1,顺序开启7个线程,如果是顺序执行 那么就会
ID: 1 No. 1 线程1执行完后,在执行线程二 输出 就是 No.1 No.2 No.3 No.1 No.2 No.3这样
ID: 1 No. 1 实际中是同时运行了线程1-7的No.1 看起来并行的执行了七个线程
ID: 2 No. 1
ID: 2 No. 1
ID: 2 No. 1
ID: 2 No. 1
ID: 2 No. 2
ID: 1 No. 2
ID: 2 No. 2
ID: 2 No. 2
ID: 1 No. 2
ID: 2 No. 2
ID: 1 No. 2
ID: 1 No. 3
ID: 1 No. 3
ID: 2 No. 3
ID: 1 No. 3
ID: 2 No. 3
ID: 2 No. 3
ID: 2 No. 3
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class ThreadDrawJ extends JFrame
{
MovingShape [] shapes;
public void init()
{
setLayout(null);
setSize(426,426);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
shapes=new MovingShape[10];
for(int i=0;i<shapes.length;i++) {
shapes[i]=new MovingShape[10];
shapes[i].start();
}
}
public static void main(String [] args)//加入Mina之前使之能当application引用
{
ThreadDrawJ f=new ThreadDrawJ();
f.init();
}
}
class MovingShape extends Thread{
private int size=100;
private int speed=10;
private Color color;
private int type;
private int x,y,w,h,dx,dy;//dx dy每次移动的距离
protected java.awt.Component app;
public boolean stopped;
MovingShape(JFrame app){
this.app=app;
x=(int)(Math.random()*app.getSize().width);
y=(int)(Math.random()*app.getSize().height);
w=(int)(Math.random()*size);
h=(int)(Math.random()*size);
dx=(int)(Math.random()*speed);
dy=(int)(Math.random()*speed);
color=new Color(
(int)(Math.random()*128+128),
(int)(Math.random()*128+128),
(int)(Math.random()*128+128));
type=(int)(Math.random()*3);
}
public void run() {
while(true) {
if(stopped) break;
//draw();
SwingUtilities.invokeLater(()->{draw();
});
try {
Thread.sleep(130);
}catch(InterruptedException e) {}
}
}
void draw() {//绘制矩形等
x+=dx;
y+=dy;
if(x<0||x+w>app.getSize().width)dx=-dx;
if(y<0||y+h>app.getSize().height)dy=-dy;
Graphics g=app.getGraphics();
switch(type) {
case 0:
g.setColor(color);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w, h);
break;
case 1:
g.setColor(color);
g.fillOval(x, y, w, h);
g.setColor(color.black);
g.drawOval(x, dy, w, h);
break;
case 2:
g.setColor(color);
g.fillRoundRect(x, y, w, h, w/5, h/5);
g.setColor(color.black);
g.drawRoundRect(x, y, w, h, w/5, h/5);
}
}
}
结果,几个矩形同时运动