线程的概念
一个程序中的方法有几条执行路径, 就有几个线程
线程的创建:
两种方式:
1, 继承Thread类
class TestThread extends Thread {......}
2, 实现Runnable接口, 然后作为参数传入到Thread类的构造方法中
class TestThread implements Runnable {......}
线程的启动:
调用线程类中的start()方法, 不能直接调用run()方法, 直接调用run()方法那叫方法调用, 不是启动线程
线程常用方法
isAlive()
判断线程是否还活着, 调用start()之前和终止之后都是死的, 其他的都是活的
interrupt()
停止线程
getPriority()
setPriority()
设置优先级, 优先级的概念: 谁的优先级高, 谁执行的时间就多
Thread里面的默认优先级:
Thread.MIN_PRIORITY = 1
Thread.MAX_PRIORITY = 10
Thread.NORM_PRIORITY = 5
Thread.sleep(long millions)
将程序暂停一会
join()
合并线程
yield()---礼让
让出CPU执行其他线程
wait()---简单了解
notify()---简单了解
notifyAll()---简单了解
public class TextThread {
public static void main(String[] args){
MyTherad mt = new MyTherad();
// mt.start();
Thread t = new Thread(mt);
System.out.println("调用start之前:"+t.isAlive());
t.start();
System.out.println("调用start之后:"+t.isAlive());
for(int i=0;i<50;i++){
System.out.println("main"+i);
}
}
public static void m(){
for(int i=0;i<50;i++){
System.out.println("===="+i);
}
}
}
class MyTherad implements Runnable{ //实现接口
@Override
public void run() {
for(int i=0;i<50;i++){
System.out.println("===="+i);
}
}
}
/*class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<50;i++){
System.out.println("===="+i);
}
}
} */
import java.util.Date;
public class TextThread2 {
public static void main(String[] args){
MyNewThread mt = new MyNewThread();
Thread t = new Thread(mt);
t.start();
/* try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
//t.interrupt();
//t.stop();
//终止进程将run方法结束进程就结束了
//MyNewThread.flag= false;
}
}
class MyNewThread implements Runnable{
@Override
public void run() {
while(true){
System.out.println(new Date());
try{
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("线程终止");
System.out.println(e.getMessage());
}
}
}
}
线程同步
银行取钱例题
public class Text {
public int money =3000;
public void getMoney(String name,int mon){
synchronized(this){
if(money>mon && money>0){
money -= mon;
System.out.println("姓名"+name);
System.out.println("取款"+mon);
System.out.println("剩余金额:"+money);
}else{
System.out.println("姓名"+name);
System.out.println("余额不足");
System.out.println("剩余金额"+money);
}
}
}
}
public class Text2 { public static void main(String[] args){ MyTherad at = new MyTherad(); Thread t1 = new Thread(at); Thread t2 = new Thread(at); t2.setName("刘毅"); t1.setName("蒋桥"); t1.start(); t2.start(); } } class MyTherad implements Runnable{ public Text bank = new Text(); @Override public void run() { bank.getMoney(Thread.currentThread().getName(),2000); } }
死锁例题
public class Text3{
public static void main(String[] args){
Text3 d1 = new Text3();
Text3 d2 = new Text3();
d1.flag=1;
d2.flag=2;
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.start();
t2.start();
}
public static Object obj1 = new Object();
public static Object obj2 = new Object();
public int flag;
public void run() {
if (flag == 1) {
System.out.println("这是第" + flag + "个线程");
synchronized (obj1) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj2) {
System.out.println("第" + flag + "个线程结束");
}
}
}
if (flag == 2) {
System.out.println("这是第" + flag + "个线程");
synchronized (obj2) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj1) {
System.out.println("第" + flag + "个线程结束");
}
}
}
}
}