线程 第六讲 091212B

public class TestThread extends Thread {

public static void main(String[] args) {
TestThread tt=new TestThread();
tt.start();
for (int i = 0; i <100; i++) {
System.out.println(i+"22222222");
}
}

@Override
public void run() {
for (int i = 0; i <100; i++) {
System.out.println(i+"aaaaaaaaaa");
}

}

}
输出:(i+"22222222"和i+"aaaaaaaaaa"交替输出)
-------------------------------------------------------------------------
public class TestThread2 {
public static void main(String[] args) {
Thread2 t2=new Thread2();
Thread t=new Thread(t2);
t.start();
for (int i = 0; i < 100; i++) {
System.out.println("-------------------");
}
}
}

class Thread2 implements Runnable {//多线程接口的实现,推荐使用这种方法。

@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("子" + i);
}
}
}
输出:("-------------------"和"子" + i交替输出)
-------------------------------------------------------------------------
public class TestThread3 {
public static void main(String[] args) throws InterruptedException {
Thread3 t3 = new Thread3("the3name");//为线程命名
t3.setName("the3newname");//为线程重命名
t3.start();
// t3.join();
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
System.out.println(t3.getName()+i);
if (i==4) {
t3.join(); //join合并进来,不按照线程进行,而按照顺序。
}
}
}
}

class Thread3 extends Thread {
public Thread3(String s) {
super(s);
}

@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子。"+i);
}
}
}
输出:
子。0
the3newname0
子。1
the3newname1
the3newname2
子。2
子。3
the3newname3
the3newname4
子。4
子。5
子。6
子。7
子。8
子。9
the3newname5
the3newname6
the3newname7
the3newname8
the3newname9
-------------------------------------------------------------------------
public class TestThread4 {
public static void main(String[] args) {
Thread4 t41 = new Thread4("W1----");
Thread4 t42=new Thread4("M2++");
t41.start();
t42.start();
for (int i = 0; i < 100; i++) {
System.out.println("。。。main"+i);
}
}
}

class Thread4 extends Thread {
public Thread4(String s) {
super (s);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+i);
if(i%5==0){
Thread.yield();//当I为5的整倍数时候,线程被迫中止。
}
}
}
}
-------------------------------------------------------------------------
import java.util.Timer;
import java.util.TimerTask;

class Task1 extends TimerTask {
@Override
public void run() {
System.out.println("1");
}
}
class Task2 extends TimerTask {
@Override
public void run() {
System.out.println("2");
}
}
class Task3 extends TimerTask {
@Override
public void run() {
System.out.println("3");
}
}

public class TestTimeTask {//定时器
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new Task2(),1000, 1000); //构造函数有多种写法表达的意义不同
}
}
-------------------------------------------------------------------------


//死锁
class TestDeadLock implements Runnable {
public int flag = 1;
static Object o1 = new Object(), o2 = new Object();
public void run() {
System.out.println("flag=" + flag);
if(flag == 1) {
synchronized(o1) {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(o2) {
System.out.println("1");
}
}
}
if(flag == 0) {
synchronized(o2) {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(o1) {
System.out.println("0");
}
}
}
}

public static void main(String[] args) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值