public class 初识线程 {
public static void main(String[] args) {
// TODO Auto-generated method stub
## // 通过继承创建线程
/*
* Demo1 d = new Demo1(); d.start();
*/
## // 通过接口创建线程
/*
* Demo2 d=new Demo2(); Thread t1=new Thread(d,"t1"); Thread t2=new
* Thread(d,"t2"); System.out.println("t1:" + t1.isAlive()); t1.start();
* System.out.println("t1:" + t1.isAlive()); t2.start(); //不安全 t1.stop();
*/
## // 线程优先级
/*
* Demo2 d = new Demo2(); Thread t1 = new Thread(d, "t1--"); Thread t2 = new
* Thread(d, "t2--"); t1.start(); t2.start();
* t1.setPriority(Thread.MAX_PRIORITY); t1.setPriority(Thread.MIN_PRIORITY);
*/
## // 线程休眠
/*
* Ticket t = new Ticket(); Thread t1=new Thread(t); t1.setName("t1");
* t1.start(); Thread t2=new Thread(t); t2.setName("t2"); t2.start();
*/
## // 线程让步
/*
* rangbu t = new rangbu(); Thread t1 = new Thread(t); Thread t2 = new
* Thread(t); t1.start(); t2.start(); for (int x = 0; x < 5; x++) {
* System.out.println("main..." + x); }
*/
## // 线程插队
/*
* Demo3 jd = new Demo3(); Thread t1 = new Thread(jd); Thread t2 = new
* Thread(jd); t1.setName("排队队员"); t2.setName("插队队员"); t1.start(); t2.start();
* try { t2.join(); } catch (InterruptedException e) { e.printStackTrace(); }
*/
## // 同步代码块
/*
* Demo4 d = new Demo4(); Thread t1 = new Thread(d, "张三"); Thread t2 = new
* Thread(d, "李四"); t1.start(); t2.start();
*/
## //线程死锁
new DieLock(true).start();
new DieLock(false).start();
}
}
//线程死锁
class DieLock extends Thread {
private boolean flag;
static Object objA = new Object();
static Object objB = new Object();
public DieLock(boolean flag) {
this.flag = flag;
}
public void run() {
if (flag) {
synchronized (objA) {
System.out.println("true -- objA");
synchronized (objB) {
System.out.println("true -- objB");
}
}
} else {
synchronized (objB) {
System.out.println("false -- objB");
synchronized (objA) {
System.out.println("false -- objA");
}
}
}
}
}
//同步代码块
class Demo4 implements Runnable {
private Object obj = new Object();
public void run() {
synchronized (obj) {
System.out.println(Thread.currentThread().getName() + "进入洗手间,门以锁上");
for (int i = 1; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "正在使用洗手间" + i);
if (i == 3) {
System.out.println(Thread.currentThread().getName() + "用完,准备出去,锁打开");
break;
}
}
}
}
}
//线程插队
class Demo3 implements Runnable {
public void run() {
for (int x = 0; x < 5; x++) {
System.out.println(Thread.currentThread().getName() + "---" + x);
}
}
}
//通过继承创建线程
class Demo1 extends Thread {
public void run() {
for (int x = 0; x < 100; x++) {
System.out.println("Demo1:" + x);
}
}
}
//通过接口创建线程
class Demo2 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + " " + x);
}
}
}
//线程休眠
class Ticket extends Thread {
private int ticket = 100;
public void run() {
while (true) {
if (ticket > 0) {
try {
Ticket.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售卖" + (100 - ticket--));
}
}
}
}
//线程让步
class rangbu implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
Thread.yield();
System.out.println(Thread.currentThread().getName() + "..." + i);
}
}
}
Java Thread线程创建方式
最新推荐文章于 2024-08-07 17:51:08 发布