java多线程简单的小例子
程序员文章站
2022-04-08 17:09:59
...
java有两种方式实现多线程,一种是继承Thread类,一种实现Runnable接口。
[img]http://dl2.iteye.com/upload/attachment/0122/8525/8e73a07e-dab4-32b8-b471-887a7cbb0552.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0122/8529/a237f2d2-fcc1-3edc-a94e-fa2b4adec905.png[/img]
[size=medium]推荐使用实现Runnable接口,因为java单继承,但是可以实现多个接口,而且Runnable会使资源共享[/size]
[img]http://dl2.iteye.com/upload/attachment/0122/8539/8c234302-a0f5-3e89-b745-5566dbf13b77.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0122/8541/b3301d65-c78f-3c44-b2e8-2ce9a8451bcb.png[/img]
package com.hgf.thread;
public class ExtThread extends Thread {
private String name;
public ExtThread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("线程开始:" + name + ",i=" + i);
}
}
public static void main(String[] args) {
ExtThread mt1 = new ExtThread("线程a");
ExtThread mt2 = new ExtThread("线程b");
//run 会先执行完线程a在执行线程b
mt1.run();
mt2.run();
}
}
[img]http://dl2.iteye.com/upload/attachment/0122/8525/8e73a07e-dab4-32b8-b471-887a7cbb0552.png[/img]
package com.hgf.thread;
public class ExtThread extends Thread {
private String name;
public ExtThread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("线程开始:" + name + ",i=" + i);
}
}
public static void main(String[] args) {
ExtThread mt1 = new ExtThread("线程a");
ExtThread mt2 = new ExtThread("线程b");
//start 会交叉运行
mt1.start();
mt2.start();
}
}
[img]http://dl2.iteye.com/upload/attachment/0122/8529/a237f2d2-fcc1-3edc-a94e-fa2b4adec905.png[/img]
package com.hgf.thread;
public class ImpRunnable implements Runnable {
private String name;
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
System.out.println("线程开始:" + name + ",i=" + i);
}
}
public ImpRunnable(String name){
this.name = name;
}
public static void main(String[] args) {
ImpRunnable imp = new ImpRunnable("线程a");
ImpRunnable imp2 = new ImpRunnable("线程b");
new Thread(imp).start();
new Thread(imp2).start();
}
}
[size=medium]推荐使用实现Runnable接口,因为java单继承,但是可以实现多个接口,而且Runnable会使资源共享[/size]
package com.hgf.thread;
public class ThreadTicket extends Thread {
private int ticket = 10;
public void run() {
for (int i = 0; i < 20; i++) {
if (this.ticket > 0) {
System.out.println("卖票:ticket" + ticket--);
}
}
}
public static void main(String[] args) {
ThreadTicket mt1=new ThreadTicket();
ThreadTicket mt2=new ThreadTicket();
ThreadTicket mt3=new ThreadTicket();
mt1.start();//每个线程都各卖了10张,共卖了30张票
mt2.start();//但实际只有10张票,每个线程都卖自己的票
mt3.start();//没有达到资源共享
}
}
[img]http://dl2.iteye.com/upload/attachment/0122/8539/8c234302-a0f5-3e89-b745-5566dbf13b77.png[/img]
package com.hgf.thread;
public class ThreadTicket implements Runnable {
private int ticket = 10;
public void run() {
for (int i = 0; i < 20; i++) {
if (this.ticket > 0) {
System.out.println("卖票:ticket" + ticket--);
}
}
}
public static void main(String[] args) {
ThreadTicket mt=new ThreadTicket();
new Thread(mt).start();//同一个mt,但是在Thread中就不可以,如果用同一 个实例化对象mt,就会出现异常
new Thread(mt).start();
new Thread(mt).start();
}
}
[img]http://dl2.iteye.com/upload/attachment/0122/8541/b3301d65-c78f-3c44-b2e8-2ce9a8451bcb.png[/img]
上一篇: Python读取文件常用的几种简单小操作