初認識マルチスレッド

4056 ワード

                  ,                 java   

時、以前から歌を聴きながらWebページを閲覧するなど、同時にパソコンでいろいろなことをしていたのがおかしいと思っていたのですが、今やっと分かりました.現
スレッドについてはまだよく知られていませんが、このようにcpuを奪う戦いを定義することができ、誰が先にcpuリソースを奪うか、誰が先に運ぶことができます.
いいですよ.スレッドにはシステムから与えられたクラスThreadがあるので,このクラスを直接継承しrun()メソッドを書き換えることができ,最後に
オブジェクトからstart()メソッドを呼び出す
 
例をあげましょう:小さなボールが上から落ちてマウスで制御された板に落ちたら弾き返しますが、弾き始めました.
高さが小さくなって、このように繰り返します;動力がなくなるまで、次はコードです.
package com.lw20130715;

import java.awt.Graphics;

import javax.swing.JFrame;

/**
 *          
 * @author Administrator
 *
 */

public class JugleBall extends JFrame{
	//         
	private Graphics g;
	
	public void showUI(){
		this.setTitle("     ");
		this.setSize(500,500);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		this.setVisible(true);
		g = this.getGraphics();

		MouseMotion mm = new MouseMotion(g,this);
		this.addMouseMotionListener(mm);
		BallThread go = new BallThread(g,mm,this,235);
//		BallThread go02 = new BallThread(g,mm,this,200);
		go.start();
//		go02.start();
	}
	//c          
	public void repaint(){
		super.paint(g);
	}
}

上は小球運動のインターフェースです
package com.lw20130715;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class MouseMotion implements MouseMotionListener{

	private int x;
	private int y;
	private Graphics g;
	private JugleBall jugleBall;
	public MouseMotion(Graphics g, JugleBall jugleBall) {
		// TODO Auto-generated constructor stub
		this.g = g;
		this.jugleBall = jugleBall;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public int getX1(){
		return this.x;
	
	}
	public int getY1(){
	
		return this.y;
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		x = e.getX();
		y = e.getY();
		this.jugleBall.repaint();
		if(450>=x&&x>=50){
			g.fillRect(x-50, 485, 100, 15);
		}else if(x<50){
			g.fillRect(0, 485, 100, 15);
		}else if(x>=450){
			g.fillRect(400, 485, 100, 15);
		}
	}

}

 
上はマウスリスナーで、主に下のロッドを制御し、マウス座標を提供しています.
package com.lw20130715;

import java.awt.Graphics;

import javax.swing.JFrame;

/**
 *          
 * @author Administrator
 *
 */

public class JugleBall extends JFrame{
	//         
	private Graphics g;
	
	public void showUI(){
		this.setTitle("     ");
		this.setSize(500,500);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		this.setVisible(true);
		g = this.getGraphics();

		MouseMotion mm = new MouseMotion(g,this);
		this.addMouseMotionListener(mm);
		BallThread go = new BallThread(g,mm,this,235);
//		BallThread go02 = new BallThread(g,mm,this,200);
		go.start();
//		go02.start();
	}
	//c          
	public void repaint(){
		super.paint(g);
	}
}

上はスレッドクラスです
最後にメインプログラムエントリクラスです.コードは次の通りです.
package com.lw20130715;
/**
 *          
 * @author Administrator
 *
 */

public class Manager {
	
	public static void main(String[]args){
		
		JugleBall ball = new JugleBall();
		ball.showUI();
		
	}

}

 
最後にプログラム実行の画像を添付します