/**
* 创建线程的3种方式
* 1.继承Thread类
* 2.实现Runnable接口
* 3.实现Callable接口
* 4.一个进程可以有多个线程、一个线程对应一个进程
* 5.防止多线程数据共享和超卖 ==》可以加锁、或者加同步代码块==》synchronized (this)
*/
package com.duoceshi.thread; public class RunningMan extends Thread{ private String name; public RunningMan(String name){ //创建一个构造函数 this.name = name; //初始化 } //重写Thread类里面的run方法==》用Ctrl+alt+空格键 @Override public void run() { for (int i = 0; i <100 ; i++) { System.out.println(this.name + " run " +i+" m"); } } }
package com.duoceshi.thread; public class RunningWoman implements Runnable { private String name; public RunningWoman(String name){ this.name = name; } //RunningWoman实现Runnable接口、需要实现接口里面的抽象方法 public void run() { for(int i=0;i<100;i++){ System.out.println(this.name + " run " + i + " m"); } } }
package com.duoceshi.thread; /** * 创建线程的3种方式 * 1.继承Thread类 * 2.实现Runnable接口 * 3.实现Callable接口 * 4.一个进程可以有多个线程、一个线程对应一个进程 * 5.防止多线程数据共享和超卖 ==》可以加锁、或者加同步代码块==》synchronized (this) */ public class ThreadTest{ public static void main(String[] args) { ThreadTest threadTest = new ThreadTest(); // threadTest.threadTest(); threadTest.runnableTest(); } public void threadTest(){ //创建了3个线程 RunningMan runningMan1 = new RunningMan("博尔特"); RunningMan runningMan2 = new RunningMan("刘翔"); RunningMan runningMan3 = new RunningMan("苏炳添"); runningMan1.start(); //谁先说后看CPU的随机性 runningMan2.start(); runningMan3.start(); } public void runnableTest(){ /** * 把RunningWoman的匿名对象作为Thread类的构造函数的参数传入进来 * 实现Runnable接口优点就是:避免了单继承的局限性、在定义线程的时候建议使用此种方式 */ Thread threadA = new Thread(new RunningWoman("博尔特")); Thread threadB = new Thread(new RunningWoman("刘翔")); Thread threadC = new Thread(new RunningWoman("苏炳添")); threadA.start(); threadB.start(); threadC.start(); } }
package com.duoceshi.thread; /** * 开发一个简单的卖票程序、可以实现多个窗口卖票 * 创建3个线程同时购票 */ public class Ticket implements Runnable{ private int ticketNum = 100; //定义100张火车票 票的库存 /** * 用这种方法有一个问题就是:在线程1购买票的时候CPU已经完成切换去处理线程2购票了 * 然后如此循环导致还没有做ticketNum减票的操作、这样会导致线程之间会共享一份数据 * 所以针对这种情况需要进行加锁、或者加入同步代码块 */ // public void run() { // while (true){ // try { // Thread.sleep(100L); // } catch (InterruptedException e) { // e.printStackTrace(); // } // if(ticketNum<=0){ // break; // } // System.out.println(Thread.currentThread()+"购票后还剩余"+ticketNum); // ticketNum --; // } // } public void run() { while (true){ try { Thread.sleep(100L); } catch (InterruptedException e) { e.printStackTrace(); } //this表示当前的对象、加锁是为了线程安全当上个线程还没结束的时候先跑完上个线程 //再跑接下来的线程 synchronized (this){ if(ticketNum<=0){ break; } System.out.println(Thread.currentThread().getName()+"卖票、还剩余票数为:"+ticketNum); ticketNum --; } } } }
package com.duoceshi.thread; public class TicketTest { public static void main(String[] args) { //创建一个对象、3个线程共享 Ticket ticket = new Ticket(); Thread thread1 = new Thread(ticket); Thread thread2 = new Thread(ticket); Thread thread3 = new Thread(ticket); thread1.start(); thread2.start(); thread3.start(); } }
标签:RunningMan,杭州,多测师,Thread,start,线程,new,多线程,public From: https://www.cnblogs.com/xiaoshubass/p/16591045.html