1、继承Thread类
一个类只要继承了Thread类,就是多线程实现类。
必须覆写 run()方法, public void run()
在线程启动时,调用start()方法, public void start()。
class MyThread extendsThread{privateString name;public MyThread(String name){ //定义构造方法
this.name =name;
}
@Overridepublic voidrun() {for (int i = 0;i<200;i++){
System.out.println(this.name+"-->"+i);
}
}
}public classThreadDemo1 {public static voidmain(String[] args) {
MyThread myThread= new MyThread("A");
MyThread myThread1= new MyThread("B");
MyThread myThread2= new MyThread("C");
myThread.start();
myThread1.start();
myThread2.start();
}
}
运行结果:
C-->0B-->0A-->0A-->1A-->2B-->1B-->2B-->3B-->4......
C-->195C-->196C-->197C-->198C-->199
2、实现Runnable接口
class MyThread implementsRunnable{privateString name;public MyThread(String name){ //定义构造方法
this.name =name;
}
@Overridepublic voidrun() {for (int i = 0;i<200;i++){
System.out.println(this.name+"-->"+i);
}
}
}public classThreadDemo1 {public static voidmain(String[] args) {
MyThread myThread= new MyThread("A");
MyThread myThread1= new MyThread("B");
MyThread myThread2= new MyThread("C");newThread(myThread).start();newThread(myThread1).start();newThread(myThread2).start();
}
}
运行结果:
B-->0B-->1C-->0A-->0A-->1C-->1......
B-->196B-->197B-->198B-->199
3、Thread类不能实现资源共享
class MyThread1 extendsThread{private int ticket =5;
@Overridepublic voidrun() {for (int x =0;x<100;x++){if (ticket>0){
System.out.println("卖票,ticket="+this.ticket--);
}
}
}
}public classThreadDemo2 {public static voidmain(String[] args) {
MyThread1 myT= newMyThread1();
MyThread1 myT2= newMyThread1();
myT.start();
myT2.start();
}
}
运行结果:(两个线程各自卖票)
卖票,ticket=5卖票,ticket=5卖票,ticket=4卖票,ticket=3卖票,ticket=4卖票,ticket=3卖票,ticket=2卖票,ticket=2卖票,ticket=1卖票,ticket=1
4、实现Runnable接口可以实现资源共享
class MyThread1 implementsRunnable{private int ticket =5;
@Overridepublic voidrun() {for (int x =0;x<100;x++){if (ticket>0){
System.out.println("卖票,ticket="+this.ticket--);
}
}
}
}public classThreadDemo2 {public static voidmain(String[] args) {
MyThread1 myT= newMyThread1();newThread(myT).start();newThread(myT).start();newThread(myT).start();
}
}
运行结果:
卖票,ticket=5卖票,ticket=2卖票,ticket=1卖票,ticket=3卖票,ticket=4
5、取得和设置线程名称
class MyThread2 implementsRunnable{
@Overridepublic voidrun() {
System.out.println(Thread.currentThread().getName());
}
}public classThreadNameDemo1 {public static voidmain(String[] args) {
MyThread2 my= newMyThread2();new Thread(my,"A").start();new Thread(my).start(); //自动命名
new Thread(my,"B").start();newThread(my).start();newThread(my).start();
}
}
运行结果:
Thread-0Thread-2Thread-1B
A
6、主方法也是线程
class MyThread2 implementsRunnable{
@Overridepublic voidrun() {
System.out.println(Thread.currentThread().getName());
}
}public classThreadNameDemo1 {public static voidmain(String[] args) {
MyThread2 my= newMyThread2();new Thread(my,"A").start();new Thread(my).start(); //自动命名
new Thread(my,"B").start();
my.run();
}
}
运行结果:
main
B
A
Thread-0
7、线程的休眠
class MyThread2 implementsRunnable{
@Overridepublic voidrun() {for (int x = 0; x<5;x++){try{
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}public classThreadNameDemo1 {public static voidmain(String[] args) {
MyThread2 my= newMyThread2();new Thread(my,"A").start();new Thread(my,"B").start();new Thread(my,"C").start();new Thread(my,"D").start();
}
}
运行结果:
A,x=0D,x=0C,x=0B,x=0A,x=1C,x=1D,x=1B,x=1C,x=2D,x=2A,x=2B,x=2D,x=3A,x=3C,x=3B,x=3C,x=4D,x=4A,x=4B,x=4
8、线程优先级
class MyThread2 implementsRunnable{
@Overridepublic voidrun() {for (int x = 0; x<10;x++){try{
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
}public classThreadNameDemo1 {public static voidmain(String[] args) {
MyThread2 my= newMyThread2();
Thread thread= new Thread(my,"A");
Thread thread1= new Thread(my,"B");
Thread thread2= new Thread(my,"C");
thread.setPriority(Thread.MAX_PRIORITY);//10
thread1.setPriority(Thread.MIN_PRIORITY); //1
thread2.setPriority(Thread.NORM_PRIORITY); //5
thread.start();
thread1.start();
thread2.start();
}
}
9、问题的引出
class MyThread1 implementsRunnable{private int ticket =5;
@Overridepublic voidrun() {for (int x =0;x<20;x++){if (ticket>0){try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票,ticket="+this.ticket--);
}
}
}
}public classThreadDemo2 {public static voidmain(String[] args) {
MyThread1 my= newMyThread1();new Thread(my,"A").start();new Thread(my,"B").start();new Thread(my,"C").start();
}
}
运行结果:
A卖票,ticket=4C卖票,ticket=5B卖票,ticket=3C卖票,ticket=2B卖票,ticket=1A卖票,ticket=0C卖票,ticket=-1
10、使用同步解决问题
同步代码块:
class MyThread1 implementsRunnable{private int ticket =59;
@Overridepublic voidrun() {for (int x =0;x<200;x++){synchronized (this){ //当前对象只允许一个对象进入
if (ticket>0){try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票,ticket="+this.ticket--);
}
}
}
}
}public classThreadDemo2 {public static voidmain(String[] args) {
MyThread1 my= newMyThread1();new Thread(my,"A").start();new Thread(my,"B").start();new Thread(my,"C").start();new Thread(my,"D").start();
}
}
同步方法:
class MyThread1 implementsRunnable{private int ticket =59;
@Overridepublic voidrun() {for (int x =0;x<200;x++){this.sale(); //调用同步方法
}
}public synchronized void sale(){ //同步方法
if (ticket>0){try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票,ticket="+this.ticket--);
}
}
}public classThreadDemo2 {public static voidmain(String[] args) {
MyThread1 my= newMyThread1();new Thread(my,"A").start();new Thread(my,"B").start();new Thread(my,"C").start();new Thread(my,"D").start();
}
}
11、生产者-消费者
classInfo{privateString name;privateString content;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}
}class Pro implementsRunnable{privateInfo info;publicPro(Info info){this.info =info;
}
@Overridepublic voidrun() {for (int x = 0; x<50;x++){if (x%2==0){this.info.setName("cathy");try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.setContent("student");
}else{this.info.setName("w");try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.setContent("wo");
}
}
}
}class Cus implementsRunnable{privateInfo info;publicCus(Info info){this.info =info;
}
@Overridepublic voidrun() {for (int x = 0;x<50;x++){try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.info.getName()+"-->"+this.info.getContent());
}
}
}public classProCus_ThreadDemo1 {public static voidmain(String[] args) {
Info info= newInfo();new Thread(newPro(info)).start();new Thread(newCus(info)).start();
}
}
运行结果:
w-->student
w-->wo
w-->student
w-->student
w-->student
w-->student
cathy-->wo
cathy-->wo
.........
产生问题:1)错位 2)重复生产,重复取出
解决错位问题——加入同步
classInfo{privateString name;privateString content;public synchronized voidset(String name,String content){this.setName(name);try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}this.setContent(content);
}public synchronized voidget(){try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContent());
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}
}class Pro implementsRunnable{privateInfo info;publicPro(Info info){this.info =info;
}
@Overridepublic voidrun() {for (int x = 0; x<50;x++){if (x%2==0){try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.set("cathy","student");
}else{try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.set("w","wo");
}
}
}
}class Cus implementsRunnable{privateInfo info;publicCus(Info info){this.info =info;
}
@Overridepublic voidrun() {for (int x = 0;x<50;x++){try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.get();
}
}
}public classProCus_ThreadDemo1 {public static voidmain(String[] args) {
Info info= newInfo();new Thread(newPro(info)).start();new Thread(newCus(info)).start();
}
}
解决重复问题——加入等待与唤醒
classInfo{privateString name;privateString content;private boolean flag =true; //flag=true表示可以生产,但是不能取走。flag=false表示不可以生产,可以取走
public synchronized voidset(String name,String content){if (!flag){try{super.wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}this.setName(name);try{
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}this.setContent(content);
flag= false;super.notify();
}public synchronized voidget(){if(flag){try{super.wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}try{
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContent());
flag= true;super.notify();
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}
}class Pro implementsRunnable{privateInfo info;publicPro(Info info){this.info =info;
}
@Overridepublic voidrun() {for (int x = 0; x<100;x++){if (x%2==0){try{
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.set("cathy","student");
}else{try{
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.set("w","wo");
}
}
}
}class Cus implementsRunnable{privateInfo info;publicCus(Info info){this.info =info;
}
@Overridepublic voidrun() {for (int x = 0;x<100;x++){try{
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}this.info.get();
}
}
}public classProCus_ThreadDemo1 {public static voidmain(String[] args) {
Info info= newInfo();new Thread(newPro(info)).start();new Thread(newCus(info)).start();
}
}