1.两种方式实现多线程
一种是继承Thread类,一种是实现Runnable接口;
Thread类是在java.lang包中定义的。一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限
class MyThread extends Thread{
private String name;
public MyThread(String name) {
super();
this.name = name;
}
public void run(){
for(int i=0;i<10;i++){
System.out.println("线程开始:"+this.name+",i="+i);
}
}
}
public class ThreadDemo01 {
public static void main(String[] args) {
MyThread mt1=new MyThread("线程a");
MyThread mt2=new MyThread("线程b");
mt1.run();
mt2.run();
}
}
此时结果很有规律,先第一个对象执行,然后第二个对象执行,并没有相互运行。在JDK的文档中可以发现,一旦调用start()方法,则会通过 JVM找到run()方法。
start()方法启动线程:
public class ThreadDemo01 {
public static void main(String[] args) {
MyThread mt1=new MyThread("线程a");
MyThread mt2=new MyThread("线程b");
mt1.start();
mt2.start();
}
};
Runnable接口
在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口完成。
class MyThread implements Runnable{
private String name;
public MyThread(String name) {
this.name = name;
}
public void run(){
for(int i=0;i<100;i++){
System.out.println("线程开始:"+this.name+",i="+i);
}
}
};
但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable targer)
此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。(start()可以协调系统的资源):
public class ThreadDemo01 {
public static void main(String[] args) {
MyThread mt1=new MyThread("线程a");
MyThread mt2=new MyThread("线程b");
new Thread(mt1).start();
new Thread(mt2).start();
}
}
两种实现方式的区别和联系:
在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处:
->避免点继承的局限,一个类可以继承多个接口。
->适合于资源的共享
来源:http://xuyuanshuaaa.iteye.com/blog/1109498 谢谢作者
2.一些多线程的例子
A 1 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
由于线程执行的不确定性,要保证这样有序的输出,必须控制好多线程的同步。
线程同步有两种基本方法:
(1) synchronized
(2) wait,notify,notifyAll
现在分别采用这两种方法来解答这道题目。
/**
*/
/**
*采用多线程技术打印10次“ABC”,即“ABCABC...”
* 实现方式(一)利用synchronized关键字实现
*/
public class XunleiInterviewMultithread {
public static void main(String[] args) {
XunleiLock lock = new XunleiLock();
new Thread(new XunleiPrinter("A", lock)).start();
new Thread(new XunleiPrinter("B", lock)).start();
new Thread(new XunleiPrinter("C", lock)).start();
}
}
class XunleiPrinter implements Runnable {
private String name = "";
private XunleiLock lock = null;
private int count = 10;
public XunleiPrinter(String name, XunleiLock lock) {
this.name = name;
this.lock = lock;
}
@Override
public void run() {
while (count > 0) {
synchronized (lock) {
if (lock.getName().equalsIgnoreCase(this.name)) {
System.out.print(name);
count--;
if (this.name.equals("A")) {
lock.setName("B");
} else if (this.name.equals("B")) {
lock.setName("C");
} else if (this.name.equals("C")) {
lock.setName("A");
}
}
}
}
}
}
class XunleiLock {
public String name = "A";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法(二)线程类修改如下,其他类一样:
class XunleiPrinter2 implements Runnable {
private String name = "";
private XunleiLock lock = null;
private int count=10;
public XunleiPrinter2(String name, XunleiLock lock) {
this.name = name;
this.lock = lock;
}
@Override
public void run() {
while(count>0) {
synchronized (lock) {
while(!lock.getName().equalsIgnoreCase(this.name)) {
try{
lock.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.print(name);
count--;
if (this.name.equals("A")) {
lock.setName("B");
} elseif (this.name.equals("B")) {
lock.setName("C");
} elseif (this.name.equals("C")) {
lock.setName("A");
}
lock.notifyAll();
}
}
}
}