标签:io ar 使用 java for sp 数据 on art
【线程的基本概念】public class TestJoin extends Thread{
public static void main(String[] args) {
MyThread t1 = new MyThread("t1");
t1.start();
try {t1.join();}
catch (InterruptedException e) {}
for(int i=1;i<=10;i++) {
System.out.println("i am main thread");
}
}
}
class MyThread extends Thread {
MyThread(String s) {super(s);}
public void run() {
for(int i=1;i<=10;i++) {
System.out.println("i am "+getName());
try {sleep(1000);}
catch (InterruptedException e) {return;}
}
}
}public class TestYield {
public static void main(String[] args) {
MyThread t1 = new MyThread("t1");
MyThread t2 = new MyThread("t2");
t1.start(); t2.start();
}
}
class MyThread extends Thread {
MyThread(String s) {super(s);}
public void run() {
for(int i=1;i<=100;i++) {
System.out.println(getName()+": "+i);
if(i%10==0) {
yield();
}
}
}
}public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 3);
t1.start();
t2.start();
}
}
class T1 implements Runnable {
public void run() {
for(int i=0;i<100;i++) {
System.out.println("T1: "+i);
}
}
}
class T2 implements Runnable {
public void run() {
for(int i=0;i<100;i++) {
System.out.println("------T2: "+i);
}
}
}public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run() {
timer.add(Thread.currentThread().getName());
}
}
class Timer {
private static int num = 0;
public synchronized void add(String name) {
//synchronized (this) {
num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+", 你是第"+num+"个使用timer的线程");
//}
}
}public 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();
}
}
标签:io ar 使用 java for sp 数据 on art
原文地址:http://blog.csdn.net/lasolmi/article/details/40212161