進程
線程的特點
1.進程是資源分配的最小單位,線程是最小的執行單位
2.一個進程可以有多個線程
3.線程共享進程資源?
package twentyth;
?
?
public class ThreadTest extends Thread {
?? ?public void run() {
?? ??? ?for (int i = 1; i <= 10; i++) {//繼承重寫方法
?? ??? ??? ?System.out.print(i + " ");
?? ??? ?}
?? ?}
?
?? ?public static void main(String[] args) {
?? ??? ?ThreadTest t = new ThreadTest();
?? ??? ?t.start();
?? ?}
}
//例題20.1
線程的實現
package twentyth;
?
import java.awt.Container;
import javax.swing.*;
?
public class SwingAndThread extends JFrame {
?? ?int count = 0; // 圖標橫坐標
?
?? ?public SwingAndThread() {
?? ??? ?setBounds(300, 200, 250, 100); // 絕對定位窗體大小與位置
?? ??? ?Container container = getContentPane();// 主容器
?? ??? ?container.setLayout(null); // 使窗體不使用任何布局管理器
?
?? ??? ?Icon icon = new ImageIcon("src/1.gif"); // 圖標對象
?? ??? ?JLabel jl = new JLabel(icon);// 顯示圖標的標簽
?? ??? ?jl.setBounds(10, 10, 200, 50); // 設置標簽的位置與大小
?? ??? ?Thread t = new Thread() { // 定義匿名線程對象
?? ??? ??? ?public void run() {
?? ??? ??? ??? ?while (true) {
?? ??? ??? ??? ??? ?jl.setBounds(count, 10, 200, 50); // 將標簽的橫坐標用變量表示
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?Thread.sleep(500); // 使線程休眠500毫秒
?? ??? ??? ??? ??? ?} catch (InterruptedException e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?count += 4; // 使橫坐標每次增加4
?? ??? ??? ??? ??? ?if (count >= 200) {
?? ??? ??? ??? ??? ??? ?// 當圖標到達標簽的最右邊時,使其回到標簽最左邊
?? ??? ??? ??? ??? ??? ?count = 10;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?};
?? ??? ?t.start(); // 啟動線程
?? ??? ?container.add(jl); // 將標簽添加到容器中
?? ??? ?setVisible(true); // 使窗體可見
?? ??? ?// 設置窗體的關閉方式
?? ??? ?setDefaultCloseOperation(EXIT_ON_CLOSE);
?? ?}
?
?? ?public static void main(String[] args) {
?? ??? ?new SwingAndThread(); // 實例化一個SwingAndThread對象
?? ?}
}
package twentyth;
?
?
import java.awt.*;
import java.util.Random;
import javax.swing.*;
?
public class SleepMethodTest extends JFrame {
?? ?private static Color[] color = { Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN, Color.ORANGE, Color.YELLOW,
?? ??? ??? ?Color.RED, Color.PINK, Color.LIGHT_GRAY }; // 定義顏色數組
?? ?private static final Random rand = new Random(); // 創建隨機對象
?
?? ?private static Color getC() { // 獲取隨機顏色值的方法
?? ??? ?return color[rand.nextInt(color.length)];
?? ?}
?
?? ?public SleepMethodTest() {
?? ??? ?Thread t = new Thread(new Runnable() { // 創建匿名線程對象
?? ??? ??? ?int x = 30; // 定義初始坐標
?? ??? ??? ?int y = 50;
?
?? ??? ??? ?public void run() {?
?? ??? ??? ??? ?while (true) { // 無限循環
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?Thread.sleep(100); // 線程休眠0.1秒
?? ??? ??? ??? ??? ?} catch (InterruptedException e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?Graphics graphics = getGraphics(); // 獲取組件繪圖上下文對象
?? ??? ??? ??? ??? ?graphics.setColor(getC()); // 設置繪圖顏色
?? ??? ??? ??? ??? ?graphics.drawLine(x, y, 100, y++); // 繪制直線并遞增垂直坐標
?? ??? ??? ??? ??? ?if (y >= 80) {
?? ??? ??? ??? ??? ??? ?y = 50;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?});
?? ??? ?t.start(); // 啟動線程
?? ?}
?
?? ?public static void main(String[] args) {
?? ??? ?init(new SleepMethodTest(), 100, 100);
?? ?}
?
?? ?public static void init(JFrame frame, int width, int height) { // 初始化程序界面的方法
?? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?? ??? ?frame.setSize(width, height);
?? ??? ?frame.setVisible(true);
?? ?}
}
//20.3
?