1.创建线程的三种方式
1)继承Thread类,重写run方法
2)实现Runnable接口
3)实现Callable接口
1.1继承Thread类
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i <100; i++) {
System.out.println("子线程............"+i);
}
}
}
public class TestThread {
public static void main(String[] args) {
//1.创建线程对象
MyThread myThread = new MyThread();
//2.启动线程类
myThread.start();
//3.打印主线程
for (int i = 0; i < 50; i++) {
System.out.println("主线程========="+i);
}
}
}
如下面的第4步执行的结果会出现重复的,你该怎么办?
public class TestThread {
public static void main(String[] args) {
//1.创建线程对象
MyThread myThread = new MyThread();
//2.启动线程类
myThread.start();
//4.创建线程对象2
MyThread myThread2 = new MyThread();
myThread2.start();
//3.打印主线程
for (int i = 0; i < 50; i++) {
System.out.println("主线程========="+i);
}
}
}
问题答案解决
获取线程ID和线程名称
1.)在Thread的子类中调用this.getId()或this.getName()
2.)使用Thread.currentThread.getId()和Thread.currentThread.getName()
修改线程名称
1.调用线程对象的setName()方法
2.使用线程子类的构造方法赋值
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i <100; i++) {
System.out.println("线程ID:"+this.getId()+"线程名称:"+this.getName()+"子线程............"+i);
}
}
}
第二种方式
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i <100; i++) {
System.out.println("线程ID:"+Thread.currentThread().getId()+"线程名称:"+Thread.currentThread().getName()+"子线程..........."+i);
}
}
}
修改线程名称
方式1:
public class TestThread {
public static void main(String[] args) {
//1.创建线程对象
MyThread myThread = new MyThread();
//2.启动线程类
//5.修改线程名称(在启动之类修改)
myThread.setName("我是子线程1");
myThread.start();
//4.创建线程对象2
MyThread myThread2 = new MyThread();
myThread2.setName("我是子线程2");
myThread2.start();
//3.打印主线程
for (int i = 0; i < 50; i++) {
System.out.println("主线程========="+i);
}
}
}
方式2:
public class MyThread extends Thread{
public MyThread() {
// TODO Auto-generated constructor stub
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i <100; i++) {
//System.out.println("线程ID:"+this.getId()+"线程名称:"+this.getName()+"子线程............"+i);
System.out.println("线程ID:"+Thread.currentThread().getId()+"线程名称:"+Thread.currentThread().getName()+"子线程..........."+i);
}
}
}
public class TestThread {
public static void main(String[] args) {
//1.创建线程对象
MyThread myThread = new MyThread("我是子线程1");
//2.启动线程类
//5.修改线程名称(在启动之类修改)
//myThread.setName("我是子线程1");
myThread.start();
//4.创建线程对象2
MyThread myThread2 = new MyThread("我是子线程2");
//myThread2.setName("我是子线程2");
myThread2.start();
//3.打印主线程
for (int i = 0; i < 50; i++) {
System.out.println("主线程========="+i);
}
}
}
案例:卖票问题
/**
* 卖票窗口类
* @author DELL
*
*/
public class TicketWin extends Thread{
public TicketWin() {
// TODO Auto-generated constructor stub
}
public TicketWin(String name) {
super(name);
}
private int ticket=100;//票
@Override
public void run() {
//卖票功能
while(true) {
if(ticket <=0 ) {
break;
}
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张");
ticket--;
}
}
public class TestticketWin {
public static void main(String[] args) {
//创建四个卖票窗口
TicketWin w1 = new TicketWin("窗口1");
TicketWin w2 = new TicketWin("窗口2");
TicketWin w3 = new TicketWin("窗口3");
TicketWin w4 = new TicketWin("窗口4");
//启动线程
w1.start();
w2.start();
w3.start();
w4.start();
}
}