创建线程的两种方式
2017-08-25 15:18
295 查看
上一篇博客为大家讲解了什么是多线程。下面教大家如何去创建一个线程。
我们在创建线程的时候通常有两种方法,在简单的应用中这两种方法都可以满足你们的需求,在较为复杂的场景中大家视情况而定。
创建线程 有两种方式
*
* 第一种
* 一个类去继承 Thread
* 创建这个子类的对象 并且调用 start() 启动
*
* 第二种方式
* 一个类去实现Runable 接口
* 创建 实现类的对象
第一种方法
第二种方法
第二种方法与第一种方法买票经典案例对比
好了,线程的创建就介绍到这里吧。
ae73
我们在创建线程的时候通常有两种方法,在简单的应用中这两种方法都可以满足你们的需求,在较为复杂的场景中大家视情况而定。
创建线程 有两种方式
*
* 第一种
* 一个类去继承 Thread
* 创建这个子类的对象 并且调用 start() 启动
*
* 第二种方式
* 一个类去实现Runable 接口
* 创建 实现类的对象
第一种方法
public class Test2 { public static void main(String[] args) { MyThread thMyThread = new MyThread(); thMyThread.start(); MyThread myThread = new MyThread(); myThread.start(); for (int i = 0; i < 10; i++) { System.out.println("主线程"+i); } /** * 多个线程是竞争关系 * 子线程和主线程同时执行起来 互相去抢占cpu执行时间 , */ } } class MyThread extends Thread{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()+i); // 当前执行的线程 的名字 } } // 子线程; 除了主线程以外 自己 创建的其他的线程 }
第二种方法
/** * 100道题 多个同学 共同做 * * 共享资源 题 * @author Administrator * * 资源共享 * 每个线程做的操作 都是相同的操作 */ public class Test8 { public static void main(String[] args) { // 1 创建共享资源的对象 MyRunnable2 runnable2 = new MyRunnable2(); // 2 创建线程对象 Thread thread = new Thread(runnable2,"小红"); Thread thread1 = new Thread(runnable2,"小绿"); Thread thread2 = new Thread(runnable2,"小兰"); Thread thread3 = new Thread(runnable2,"小紫"); // 3 启动线程 thread.start(); thread1.start(); thread2.start(); thread3.start(); } } class MyRunnable2 implements Runnable{ int subject= 10; public void run() { //t1 t2 for (; subject >0; subject--) { System.out.println(Thread.currentThread().getName()+"做了"+(100-subject)+"还剩"+subject); } // while(true){ // if(subject>0){ // subject--; // System.out.println(Thread.currentThread().getName()+"做了"+(100-subject)+"还剩"+subject); // // }else{ // break; // } // } } }
第二种方法与第一种方法买票经典案例对比
/** * 创建线程的第二种方式 * * * 资源 票 是共享的 所以使用 实现runnable 接口 * * * */ public class Ttest7 { public static void main(String[] args) { MyRunnbale myRunnbale = new MyRunnbale(); Thread thread = new Thread(myRunnbale); Thread thread2 = new Thread(myRunnbale); Thread thread3 = new Thread(myRunnbale); Thread thread4 = new Thread(myRunnbale); thread.start(); thread2.start(); thread3.start(); thread4.start(); } } class MyRunnbale implements Runnable{ int tickt = 100; @Override public void run() { // 卖票 while(true){ if(tickt>0){ tickt--; System.out.println(Thread.currentThread().getName()+"卖了"+(100-tickt)+"还剩"+tickt+"张"); }else{ break; } } } } 第一种方法的买票 票资源不能共享 public class Test6 { public static void main(String[] args) { MyThread5 thread1 = new MyThread5("窗口一"); MyThread5 thread2 = new MyThread5("窗口二"); MyThread5 thread3 = new MyThread5("窗口三"); MyThread5 thread4 = new MyThread5("窗口四"); // 各个线程之间没有任何的联系 thread1.start(); thread2.start(); thread3.start(); thread4.start(); } } class MyThread5 extends Thread{ int ticket = 100; public MyThread5(String name) { super(name);// 修改线程的名字 } @Override public void run() {// 卖100张票 while(true){ if(ticket>0){ ticket--;// 票被卖出去一张 System.out.println(getName()+"卖了"+(100-ticket)+"张票,还剩"+ticket+"张"); // 卖出去 // Thread.currentThread().getName() }else{ break; } } } }
好了,线程的创建就介绍到这里吧。
ae73
相关文章推荐
- Java【多线程知识总结(5)】比较继承Thread类创建线程和实现Runnable接口创建线程这两种方式
- 【Java】创建线程对象两种方式
- 创建线程的两种方式
- 创建线程的两种方式
- 线程创建的两种方式02--Java基础068
- java多线程总结一:线程的两种创建方式及优劣比较
- 多线程--创建线程两种方式实例
- 黑马程序员_java创建线程的两种方式
- 创建线程的两种方式
- 从设计的角度讨论Java中线程的两种创建方式
- 创建线程的两种传统方式
- [Android面试]Java创建线程的两种方式
- java中创建线程的两种方式
- 线程的两种创建方式及比较
- Java中创建线程的两种方式
- java多线程总结一:线程的两种创建方式及优劣比较
- 创建线程的两种方式
- 创建线程的两种方式
- 创建和开启线程的两种方式