今天是复习之前学习的线程相关内容
线程的建立
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("线程运行");
}
}
public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
System.out.println("线程运行开始");
t.start();
System.out.println("线程运行结束");
}
}
运行结果:
线程的同步运行
public class Mythread extends Thread{
public Mythread(){
}
public Mythread(String name){
super(name);
}
private int tickets = 100;
@Override
public void run() {
while(tickets>0){
System.out.println(getName()+"卖出了第"+tickets--+"张票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Mythread t1 = new Mythread("售票员1");
Mythread t2 = new Mythread("售票员2");
Mythread t3 = new Mythread("售票员3");
Mythread t4 = new Mythread("售票员4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行结果:
用Runnable实现多线程
包括线程锁和join()方法
import javax.swing.plaf.SliderUI;
public class MyRunnable implements Runnable{
private int tickets = 21;
private String s = "";
@Override
public void run() {
while(tickets>0){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(tickets==0){
break;
}
synchronized(s){
System.out.println(Thread.currentThread().getName()+"卖出第"+tickets--+"张票");
}
}
}
}
public class Test {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread t1 = new Thread(runnable, "售票员1");
Thread t2 = new Thread(runnable, "售票员2");
Thread t3 = new Thread(runnable, "售票员3");
Thread t4 = new Thread(runnable, "售票员4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行结果:
public class JoinTest {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread t0 = new Thread(runnable);
t0.start();
try {
t0.join(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("程序结束");
}
}
运行结果:
银行取钱的算法
public class GetMoney implements Runnable{
private int money = 3000;
@Override
public void run() {
while(money>0){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(money==0){
break;
}
synchronized ("") {
money = money-100;
System.out.println(Thread.currentThread().getName()+"取了第"+(3000-money)/100+"个100元,剩余"+money+"元");
}
}
}
}
public class Test {
public static void main(String[] args) {
GetMoney get = new GetMoney();
Thread t1 = new Thread(get, "一号");
Thread t2 = new Thread(get, "二号");
t1.start();
t2.start();
}
}
运行结果:
死锁
public class Runnable1 implements Runnable{
private String lock1 = "123";
private String lock2 = "456";
@Override
public void run() {
synchronized (lock1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等待lock2");
synchronized (lock2) {
}
}
}
}
public class Runnable2 implements Runnable{
private String lock1 = "123";
private String lock2 = "456";
@Override
public void run() {
synchronized (lock2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等待lock1");
synchronized (lock1) {
}
}
}
}
public class Test {
public static void main(String[] args) {
Runnable1 run1 = new Runnable1();
Runnable2 run2 = new Runnable2();
Thread t1 = new Thread(run1);
Thread t2 = new Thread(run2);
t1.start();
t2.start();
}
}
运行结果: