【Java】Thread类的常用方法 获取线程的名称,设置线程名称,sleep()

Thread类的常用方法

获取线程的名称:

1.使用Thread类中的方法getName()

     String getName() 返回该线程的名称

2.可以先获取到当前正在执行的线程,使用线程中的方法getName()获取线程的名称

     static Thread currentThread() 返回对当前正在执行的线程对象的引用

public class DemoThread {

public static void main(String[] args) {

MyThread myThread = new MyThread();

//调用start方法,开启新线程

myThread.start();

new MyThread().start();

new MyThread().start();

System.out.println(Thread.currentThread().getName());

}

}
public class MyThread extends Thread{

@Override

public void run() {

Thread t = Thread.currentThread();

//System.out.println(t);

System.out.println(t.getName());

}

}

 

 

设置线程名称

1.使用Thread类中的setName()

        void setName(String name)改变线程名称,使之与参数name相同

2.创建一个带参数的构造方法,参数传递线程的名称;调用父类的带参构造方法,把线程名称传递给父类,让父类(Thread)给子线程起一个名字 Thread(String name) 分配新的Thread对象

public class DemoThread {

public static void main(String[] args) {

MyThread myThread = new MyThread("小明");

//调用start方法,开启新线程

myThread.start();

MyThread mt2 = new MyThread();

mt2.setName("旺财");

mt2.start();

}

}
public class MyThread extends Thread{

public MyThread(){}

public MyThread(String name){

super(name);

}

@Override

public void run() {

Thread t = Thread.currentThread();

//System.out.println(t);

System.out.println(t.getName());

}
}

 

 

public static void sleep(long millis):

使用当前正在执行的线程以指定的毫秒数暂停(暂时停止执行)

毫秒数结束之后,线程继续执行


public class DemoThreadSleep {

public static void main(String[] args) {

for (int i = 0; i < 20; i++) {

System.out.println(i);

try{

Thread.sleep(1000);

}catch(Exception e){

e.printStackTrace();

}

}

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java线程机制: 例子 1 public class Example1 { static Lefthand left;static Righthand right; public static void main(String args[]) { left=new Lefthand(); //创建两个线程。 right=new Righthand(); left.start(); right.start(); } } class Lefthand extends Thread { public void run() { for(int I=0;I<=5;I++) { System.out.println("I am a student"); try{sleep(500);} catch(InterruptedException e){} } } } class Righthand extends Thread { public void run() { for(int I=0;I<=5;I++) { System.out.println("I am oookkk"); try{sleep(300);} catch(InterruptedException e){} } } } 在上述例子,我们在main主线程创建了两个新的线程lefthand和righthand。当lefthand调用start()开始运行时,Lefthand的run()将自动被执行。 我们来分析一下上面程序的输出结果。Left线程首先开始执行,这时Lefthand的run方法开始执行,输出”I am a student”后,left主动“休息”500毫秒,让出了CPU。这时正在排队等待CPU的right线程的run方法马上被执行,输出“I am ookk”,right在主动让出CPU300毫秒后又来排队等待CPU服务,这时right发现left还没有“醒来”,即没有来排队抢占CPU,因此left的run方法被执行,又输出“I am oookkk“… …。程序的执行结果是: E:\dd>java Example1 I am student I am oookkk I am oookkk I am student I am oookkk I am oookkk I am student I am oookkk I am student I am oookkk I am student I am student 2.实现Runnable接口 例子 2 import java.applet.*; import java.awt.*; public class Example2 extends java.applet.Applet implements Runnable { Thread circleThread; public void start() { if (circleThread==null) { circleThread=new Thread(this); circleThread.start(); } } public void run() { while(circleThread !=null) { repaint(); try{ circleThread.sleep(1000); } catch(InterruptedException e){} } } public void paint(Graphics g) { double i=Math.random(); if(i<0.5) g.setColor(Color.red); else g.setColor(Color.blue); g.fillOval(100,100,(int)(100*i),(int)(100*i)); } public void stop() { circleThread.yield(); circleThread=null; } } 在上述例子2,我们在小程序这个主线程用构造方法Thread(this)创建了一个新的线程。This代表着小程序作为这个新的线程的目标对象,因此我们的小程序必须为这个新创建的线程实现Runnable接口,即小程序利用Runnable接口为其创建的这个新线程提供run()方法,给出该线程的操作。 首先,在小程序的start()方法构造了一个名为circleThread线程并调用线程的start()方法来启动这一线程,即在小程序的主线程又开始了一个线程:circleThread。下面的语句建立了一个新的线程: circlethread =new Thread(this); 其this作为该线程的目标对象,它必须实现Runnable接口。线程被启动以后,自动调用目标对象的run()方法,除非线程被停止。在run()方法的第十一,Applet重绘本身,然后睡眠1秒,同时要捕获异常事件并进行处理。 如果你离开这一页,程序将调用stop()方法,将线程置空。当你返回时,又会创建一个新的线程。在具体应用采用哪种方法来构造线程体要视具体情况而定。通常,当一个新的线程已继承了另一个,而想在该线程创建一个新的线程时,就应该用第二种方法来构造,即实现Rennable接口。 需要理解的是,我们的小应用程序实际上是浏览器的一个线程,这个线程由浏览器启动执行,浏览器自动调用执行小程的init()、start()方法等。因此我们要创建一个新的线程最好把新线程的启动放在小程序的start()方法。 下面的例子3是一个应用程序,这个应用程序在创建窗口的同时又创建了一个新的线程,该线程负责让窗口的一个按钮改变它的大小。 例子 3 import java.awt.*; import java.awt.event.*; public class Example3 { public static void main(String args[]) { Mywin win=new Mywin(); win.pack(); } } class Mywin extends Frame implements Runnable { Button b=new Button("ok"); int x=5; Thread bird=null; Mywin() { setBounds(100,100,120,120); setLayout(new FlowLayout()); setVisible(true); add(b); b.setBackground(Color.green); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); bird=new Thread(this); //创建一个新的线程,窗口做目标对象, //替线程bird实现接口Runnable。 bird.start(); //在创建窗口时又开始了线程dird. } public void run() { while(true) { x=x+1; if(x>100) x=5; b.setBounds(40,40,x,x); try{bird.sleep(200);} catch(InterruptedException e){} } } } 滚动字幕线程。 例子 4 import java.applet.*; import java.awt.*; public class Example4 extends java.applet.Applet implements Runnable { int x=0; Thread Scrollwords=null; public void init() { setBackground(Color.cyan); setForeground(Color.red); setFont(new Font("TimesRoman",Font.BOLD,18)); } public void start() { if(Scrollwords==null) { Scrollwords=new Thread(this); Scrollwords.start(); } } public void run() { while (Scrollwords!=null) { x=x+5; if(x>500) x=0; repaint(); try{Scrollwords.sleep(80);} catch(InterruptedException e){} } } public void paint(Graphics g) { g.drawString("欢 迎 使 用 字 典 ",x ,80); } public void stop() { Scrollwords.yield(); Scrollwords=null; } } 带滚动字幕的小字典。 例子 5 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example5 extends Applet implements ActionListener,Runnable { TextField text1,text2; int x=0; Thread Scrollwords=null; public void init() { setBackground(Color.cyan); setForeground(Color.red); setFont(new Font("TimesRoman",Font.BOLD,18)); text1=new TextField(10); text2=new TextField(10); add(new Label("输入一个英文单词:")); add(text1); add(new Label("汉语意思:")); add(text2); text1.addActionListener(this); } public void start() { if(Scrollwords==null) { Scrollwords=new Thread(this); Scrollwords.start(); } } public void run() { while (Scrollwords!=null) { x=x+5; if(x>500) x=0; repaint(); try{Scrollwords.sleep(80);} catch(InterruptedException e){} } } public void paint(Graphics g) { g.drawString("欢 迎 使 用 字 典 ",x ,120); } public void stop() { Scrollwords.yield(); Scrollwords=null; } public void actionPerformed(ActionEvent e) { if((e.getSource()==text1)&&(text1.getText().equals("boy"))) { text2.setText("男孩"); } else if((e.getSource()==text1)&&(text1.getText().equals("sun"))) { text2.setText("太阳"); } else { text2.setText("没有该单词"); } } } 下面是一个左手画圆右手画方的例子。我们在主线程创建了两个线程:left、right,其一个负责画圆,另一个负责画方。在这个例子我们使用了容器方法getGraphics()来获取一个Graphics对象(可以理解为一个画笔)。 例子 6 (效果如图1所示) 图1 双线程绘画程序 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example6 extends Applet implements Runnable { Thread left,right; Graphics mypen; int x,y; public void init() { left=new Thread(this); right=new Thread(this); x=10; y=10; mypen=getGraphics(); } public void start() { left.start(); right.start(); } public void run() { while(true) if (Thread.currentThread()==left) { x=x+1; if(x>240) x=10; mypen.setColor(Color.blue); mypen.clearRect(10,10,300,100); mypen.drawRect(10+x,10,50,50); try{left.sleep(60);} catch(InterruptedException e){} } else if(Thread.currentThread()==right) { y=y+1; if(y>240) y=10; mypen.setColor(Color.red); mypen.clearRect(10,110,300,100); mypen.drawOval(10+y,110,50,50); try{right.sleep(60);} catch(InterruptedException e){} } } public void stop() { left=null; right=null; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值