线程的操作方法

来源:互联网 发布:网络文明宣言口号 编辑:程序博客网 时间:2024/05/19 02:06
   线程的操作方法有很多种,这些方法可以使线程从某一种状态过渡到另一种状态,大致上来说线程的操作方法一共分为四种,分别是:线程的休眠、现成的添加、线程的中断以及线程的礼让,下面我们分别来加以说明。

一、线程的休眠:

源代码:
package MultiTread;
import java.awt.*;
import javax.swing.*;
import java.util.*; 
public class SleepMethod extends JFrame{
private JPanel jPanel=null;
private static Color[] color={Color.BLACK,Color.WHITE,Color.BLUE,Color.RED,Color.GREEN,Color.cyan,Color.YELLOW,Color.GRAY,Color.pink};
private static Random random=new Random();
private Thread t;
private static Color getColor(){
return color[random.nextInt(color.length)];
}
public SleepMethod(String title){
super(title);
initUI();
}
private void initUI(){
setContentPane(getJPanel());
setBounds(100,100,350,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel getJPanel(){
if(jPanel==null){
jPanel=new JPanel();
t=new Thread(new Runnable(){
int x=30, y=50;
public void run(){
while(true){
try{
t.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
Graphics graphics=getGraphics();
graphics.setColor(getColor());
graphics.drawLine(x,y,320,y++);
if(y>170){
y=50;
}
}
}
});
t.start();
}
return jPanel;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new SleepMethod("线程的休眠");
}
}

运行的结果:
线程的操作方法 - 张侦毅 - 张侦毅

现成的休眠采用的是sleep()方法,它的语法如下:
try{
t.sleep(100);
  }catch(InterruptedException e){
e.printStackTrace();
  }

       上述代码会使线程在100毫秒内不会进入就绪状态,由于sleep()方法的执行有可能抛出InterruptedException异常,所以将sleep()方法的调用放在try/catch语句中。虽然用了sleep()方法的线程在一段时间内会醒来,但是并不一定能够保证它醒来后就会进入到运行的状态,只能够保证它进入到就绪的状态。

      在SleepMethod程序的实例中我们定义了一个getColor()方法,该方法用于随机产生Color类型的对象,并且在产生线程的匿名内部类中使用getGraphics()方法来获取Graphics对象,使用该对象调用setColor()方法为图形设置颜色;调用drawLine()方法绘制一条直线,同时线段会根据纵坐标的变化做自动的调整。
 
二、线程的加入:
package MultiTread;
import java.awt.*;
import javax.swing.*;
public class JoinThread extends JFrame{
private Thread thread1,thread2;
private JPanel jPanel=null;
private JProgressBar progressBar1=new JProgressBar();
private JProgressBar progressBar2=new JProgressBar();
public JoinThread(String title){
super(title);
initUI();
}
private void initUI(){
setContentPane(getJPanel());
setBounds(100,100,270,130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel getJPanel(){
if(jPanel==null){
jPanel=new JPanel();
jPanel.setLayout(new BorderLayout());
progressBar1.setStringPainted(true);
progressBar2.setStringPainted(true);
thread1=new Thread(new Runnable(){
public void run(){
int count=0;
while(true){
try{
progressBar1.setValue(++count);
thread1.sleep(100);
thread2.join();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
});
thread1.start();
thread2=new Thread(new Runnable(){
int count=0;
public void run(){
while(true){
try{
progressBar2.setValue(++count);
thread2.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
if(count==100){
break;
}
}
}
});
thread2.start();
jPanel.add(progressBar1,BorderLayout.NORTH);
jPanel.add(progressBar2,BorderLayout.SOUTH);
}
return jPanel;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new JoinThread("线程的加入");
}
}

运行的结果:
线程的操作方法 - 张侦毅 - 张侦毅
 
       在JoinThread的实例中我们一共创建了两个线程,非别是线程thread1和线程thread2,他们分别负责相应的滚动条的滚动。在线程thread1的方法中使用join()方法加入线程thread2,这样线程thread1在执行时会首先暂停,然后直接加载线程thread2,当线程thread2执行完毕后再来执行thread1,所以说才会出现我们看到的那样,首先是下面的滚动条滚动完毕后上面的滚动条才开始接着滚动。

三、线程的终止:
package MultiTread;
import java.awt.*;
import javax.swing.*;
public class InterruptThread extends JFrame{
private JPanel jPanel=null;
private Thread thread;
private JProgressBar progressBar=new JProgressBar();
private JLabel jLabel;
public InterruptThread(String title){
super(title);
initUI();
}
private void initUI(){
setContentPane(getJPanel());
setBounds(100,100,270,130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel getJPanel(){
if(jPanel==null){
jPanel=new JPanel();
jPanel.setLayout(new BorderLayout());
jLabel=new JLabel("");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
progressBar.setStringPainted(true);
thread=new Thread(new Runnable(){
public void run(){
int count=0;
while(true){
try{
progressBar.setValue(++count);
thread.sleep(100);
}catch(InterruptedException e){
jLabel.setText("当前线程被中断!");
e.printStackTrace();
break;
}
}
}
});
thread.start();
thread.interrupt();
jPanel.add(progressBar,BorderLayout.NORTH);
jPanel.add(jLabel,BorderLayout.CENTER);
}
return jPanel;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new InterruptThread("线程的终止");
}
}

运行的结果:
线程的操作方法 - 张侦毅 - 张侦毅
 
       在InterruptThread的实例中,我们创建了InterruptThread类,在该类中我们调用了Runnable接口,之后我们创建了一个进度条和一个标签,由于本实例中还调用了interrupt()方法,所以说在程序运行时程序本身会被interrupt()方法打断,此时抛出InterruptedException异常,然后我们将异常信息直接反馈到了JLabel标签上,这就是标签上所显示的【当前线程被中断】信息。

四、线程的礼让:

       线程的礼让采用的是yield()方法,该方法具有使同样具有优先级的程序进入可执行状态的机会,当当前程序放弃执行权时会再度回归到就绪的状态。但是对于支持多任务的操作系统来说,比如说是windows系统,是不需要调用yeild()方法,因为操作系统会自动为线程分配时间片来执行,这样反而会降低了程序的运行效率。所以说在这里我们就不举例来说明了。
0 0
原创粉丝点击