`
zhuozhuobeauty
  • 浏览: 17154 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

线程总结~三水鉴定:很有用的东东

    博客分类:
  • java
 
阅读更多

一、进程与线程的区别
线程:一个程序执行多个任务,一个任务就是所谓的线程,可以运行一个以上的线程程序

称之为多线程程序
进程:简而言之,运行着的应用程序可以成为进程,注意不运行的不称之为进程
线程和进程比较:线程开销小。可共享数据。进程开销大,各个进程之间是相互独立。
二、线程的执行顺序问题
我们来看下面这样一个简单线程
ma{
mb();
md();
println("a");
}
mb{
mc();
println("b");
}
mc{
println("c");
}
md(){
println("d");
}
输出结果必然是c,b,d,a.这是一个按照顺序执行的单线程程序。
若我们想要多个任务同时运行,及实现多线程,则需要用到如下格式的代码:
public class ThreadDemo extends Thread {
            //主函数,属性方法
         }
再看如下一个程序,运行的结果是混乱的,难以预料的。
public class ThreadDemo extends Thread {

private static int count = 0;
private int id;

/**
* @param args
*/
public static void main(String[] args) {
for(int i=1;i<=5;i++){
ThreadDemo td = new ThreadDemo(i);
td.start();
System.out.println("第"+i+"号线程启动了......");
}
}

public ThreadDemo(int id) {
this.id = id;
}

public void run() {
test();
}

public void test() {
while (count <= 20) {
count++;
System.out.println("线程" + id + "正在执行,count的值

是:" + count);
}
}
为了使线程的运行有规律可循,我们通过一个Thread.sleep(n);方法来实现,其中n代表

暂停的毫秒数。但要注意,根据错误提示,要变成
               try {
Thread.sleep(id*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
三、写线程要注意的几点
1.调用线程的时候,一定要注意让线程start起来,没有start线程是不启动的
2.继承Thread类的时候要重写run方法~
四、关于弹球练习的总结
目前的小球还很多bug,非常低级,比如,弹着弹着就少了一个球。。。先贴一下代码,求大神指点

,待我修改以后再把正确的代码贴出~
**主线程
package xiancheng;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;

/**
* 小球移动界面类
*
* @author XiongXiangJun
*
*/
public class Balltry extends JFrame {

public static void main(String[] args) {
Balltry bt = new Balltry();
bt.initGUI();
}

private void initGUI() {
this.setTitle("小球移动");
this.setSize(new Dimension(600, 500));
this.setDefaultCloseOperation(3);
this.setLocation(0, 0);
this.setResizable(false);
this.setVisible(true);
ball1.start();
ball2.start();
}

Ball ball1 = new Ball(Color.BLUE, 20, 50, 30,this);
Ball2 ball2 = new Ball2(Color.YELLOW, 500, 20, 30,this);

/**
* 重写重绘方法
*/
public void paint(Graphics g) {
super.paint(g);

g.setColor(ball1.getColor());
g.fillOval(ball1.getX(), ball1.getY(), ball1.getWidth(), ball1.getWidth());
g.setColor(ball2.getColor());
g.fillOval(ball2.getX(), ball2.getY(), ball2.getWidth(), ball2.getWidth());
}

}
**继承Thread的Ball类
package xiancheng;

import java.awt.Color;
import java.util.Random;

/**
* 小球类,小球要能独立的运行,所以让它继承自Thread
*
* @author XiongXiangJun
*
*/
public class Ball extends Thread {

// 小球的颜色
private Color color;
// 小球的起始坐标和宽高
private int x, y, width;
private Balltry bt;

/**
* 构造方法
*
* @param color颜色
* @param x起始点x
* @param y起始点y
* @param width小球的宽度
* @param hegiht小球的高度
*/
public Ball(Color color, int x, int y, int width,Balltry bt) {
this.color = color;
this.x = x;
this.y = y;
this.width = width;
this.bt = bt;
}

/**
* 线程的执行方法
*/
public void run() {
boolean flag = true;
// 让小球不停的运动起来
while(true){
while (flag) {

Random rand = new Random();
x += rand.nextInt(50);
y += rand.nextInt(50);
if(x<560&&y<460){
//调用重绘方法
bt.repaint();
try {
// 让线程休眠0.5秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else
{flag = false;

}

}
while(!flag)
{
Random rand = new Random();
x -= rand.nextInt(100);
y -= rand.nextInt(100);
if(x>30&&y>30){
bt.repaint();


try {
// 让线程休眠0.5秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else flag = true;
}
}
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}



}
**继承Thread的Ball2类
package xiancheng;

import java.awt.Color;
import java.util.Random;

/**
* 小球类,小球要能独立的运行,所以让它继承自Thread
*
* @author XiongXiangJun
*
*/
public class Ball2 extends Thread {

// 小球的颜色
private Color color;
// 小球的起始坐标和宽高
private int x, y, width;
private Balltry bt;

/**
* 构造方法
*
* @param color颜色
* @param x起始点x
* @param y起始点y
* @param width小球的宽度
* @param hegiht小球的高度
*/
public Ball2(Color color, int x, int y, int width,Balltry bt) {
this.color = color;
this.x = x;
this.y = y;
this.width = width;
this.bt = bt;
}

/**
* 线程的执行方法
*/
public void run() {
boolean flag = false;
// 让小球不停的运动起来
while(true){
while (flag) {

Random rand = new Random();
x += rand.nextInt(100);
y += rand.nextInt(150);
if(x>20&&y<450){
//调用重绘方法
bt.repaint();
try {
// 让线程休眠0.5秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else
{flag = false;

}

}
while(!flag)
{
Random rand = new Random();
x -= rand.nextInt(150);
y += rand.nextInt(50);
if(x<550&&y>40){
bt.repaint();


try {
// 让线程休眠0.8秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else flag = true;
}
}
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}



}

  • 大小: 13.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics