念你的時(shí)光比相愛長

圖片發(fā)自簡書App
繼承Thread實(shí)現(xiàn)多線程
package c1;
public class ThreadText extends Thread{
private int count=10;
public void run() {
while(count!=0) {
System.out.println(count+" ");
count--;
}
}
public static void main(String[] args) {
ThreadText text=new ThreadText();
text.start();
}
}
-
運(yùn)行結(jié)果:
image.png
實(shí)現(xiàn)Runnable接口實(shí)現(xiàn)多線程
package c2;
import java.awt.Color;
import java.awt.Container;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class SwingAndThread extends JFrame{
private int count=0;
public SwingAndThread() {
Container container=getContentPane();
JLabel jl=new JLabel();
URL url=SwingAndThread.class.getResource("cc.png");
Icon icon=new ImageIcon(url);
jl.setIcon(icon);
jl.setBounds(10,10,70,80);
Thread t=new Thread(new Runnable() {
public void run() {
while(count<=200) {
jl.setBounds(count, 10, 70, 80);
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
count+=4;
if(count==200) {
count=10;
}
}
}
});
t.start();
container.add(jl);
setBounds(300,200,250,100);//橫坐標(biāo),縱坐標(biāo),長,寬
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SwingAndThread();
}
}
- 運(yùn)行結(jié)果:滾動(dòng)的圖標(biāo)

image.png
線程的生命周期
- java線程有七種狀態(tài),出生狀態(tài),就緒狀態(tài),運(yùn)行狀態(tài),等待狀態(tài),休眠狀態(tài),阻塞狀態(tài)和死亡狀態(tài)。
- 調(diào)用start()方法之前都處于出生狀態(tài)
- 調(diào)用start()方法之后為就緒狀態(tài)(可執(zhí)行狀態(tài))
- 線程得到系統(tǒng)資源后進(jìn)入運(yùn)行狀態(tài)
- 調(diào)用wait()方法進(jìn)入等待狀態(tài)
- 用notify()方法喚醒等待狀態(tài)的線程
notifyAll()方法喚醒所有等待線程 - sleep()方法進(jìn)入休眠狀態(tài)
- 如果一個(gè)線程在運(yùn)行狀態(tài)下發(fā)出 輸入/輸出 請求,該線程進(jìn)入阻塞狀態(tài),
線程等待 輸入/輸出 結(jié)束時(shí)的狀態(tài)為就緒狀態(tài) - 當(dāng)線程的run()方法執(zhí)行完畢進(jìn)入死亡狀態(tài)

image.png
-
線程的休眠:
在窗口中畫線,并產(chǎn)生隨機(jī)顏色,運(yùn)用線程休眠進(jìn)行延時(shí)
package c3;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Window;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class SleepText extends JFrame{
private Color[] color= {
Color.BLACK,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW
};
private Random rand=new Random();
private Color getC() {
return color[rand.nextInt(color.length)];
}
public SleepText() {
Thread t=new Thread(new Runnable() {
int x=30;
int y=50;
public void run() {
while(true) {
try {
Thread.sleep(100);
} 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 SleepText(),100,100);//在靜態(tài)方法中不可以調(diào)用非靜態(tài)方法
}
public static void init(JFrame frame,int wdith,int height) {
frame.setSize(wdith,height);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 在靜態(tài)方法中不可以調(diào)用非靜態(tài)方法
-
線程的加入:
package c4;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.WindowConstants;
public class JoinText extends JFrame{
private Thread t1;
private Thread t2;
final JProgressBar progressBar1=new JProgressBar();
final JProgressBar progressBar2=new JProgressBar();
public static void main(String[] args) {
init(new JoinText(),100,100);
}
public JoinText() {
super();
getContentPane().add(progressBar1, BorderLayout.NORTH);
getContentPane().add(progressBar2, BorderLayout.SOUTH);
progressBar1.setStringPainted(true);
progressBar2.setStringPainted(true);
t1=new Thread(new Runnable() {
int count=0;
public void run() {
while(true) {
progressBar1.setValue(++count);
try {
Thread.sleep(50);
if(count==50){
t2.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
t2=new Thread(new Runnable() {
int count=0;
public void run() {
while(true) {
progressBar2.setValue(++count);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count==100) {
break;
}
}
}
});
t2.start();
}
public static void init(JFrame frame, int width, int height) {
// TODO Auto-generated method stub
frame.setSize(width,height);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
運(yùn)行結(jié)果:

1.png

2.png
-
線程的同步(資源共享問題)
package c5;
public class Synchronized implements Runnable{
int num=10;
public void run() {
while(true) {
synchronized ("") {//線程的同步(資源共享問題)
if(num>0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(--num);
}
}
}
}
public static void main(String[] args) {
Synchronized sy=new Synchronized();
Thread t1=new Thread(sy);
Thread t2=new Thread(sy);
Thread t3=new Thread(sy);
Thread t4=new Thread(sy);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
public synchronized void doit() {
if(num>0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(--num);
}
}
public void run() {
while(true) {
doit();
}
}
運(yùn)行結(jié)果:

image.png
start();//線程處于就緒狀態(tài)(可執(zhí)行狀態(tài))
wait();//線程進(jìn)入等待(進(jìn)入就緒狀態(tài))
sleep();//線程進(jìn)入休眠(進(jìn)入就緒狀態(tài))
notify();//喚醒等待線程(進(jìn)入運(yùn)行狀態(tài))
notifyAll();//喚醒所以等待下的線程(進(jìn)入運(yùn)行狀態(tài))
join();//線程的加入
interrupt();//線程的中斷,使線程離開run()方法,同時(shí)結(jié)束線程,但會拋出異常
setPriority();//線程的優(yōu)先級,范圍10-1,數(shù)越大優(yōu)先級越高
synchronized(""){
//線程同步機(jī)制,多個(gè)線程訪問一個(gè)資源代碼時(shí),
//將共享的資源放在這里,在一個(gè)時(shí)間內(nèi)只有一個(gè)線程執(zhí)行共享代碼了。
}
- 線程中斷還可以在while(true){}里設(shè)置一個(gè) if 條件語句,如果成立就break;
PS:執(zhí)行代碼的時(shí)候,好幾個(gè)線程同時(shí)執(zhí)行,CPU使用率達(dá)到100%了,尤其是沒用 synchronized 訪問同一資源代碼的時(shí)候?。?!
