class TestThread1 extends Thread
{
int i;
public TestThread1()
{
i = 0;
}
public void run()
{
while(true)
{
i++;
try
{
sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
if(i == 10)
break;
System.out.println("TestThead1 run() called!");
} }
} class TestThread2 extends Thread
{
int j;
public TestThread2()
{
j = 0;
} public void run()
{
while(true)
{
j++;
System.out.println("TestThead2 run() called!");
if(j == 10)
break;
}
}
} public class Test
{
public static void main(String[] args)
{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
//如何让t1先执行,但不是先执行完毕。!
t1.setPriority(6);
t2.setPriority(7); //在这里设定 优先级好像不行,大家看看我的运行结果,和不设置优先级是一样的!
t1.start();
t2.start();
}
}
=================结果
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
{
int i;
public TestThread1()
{
i = 0;
}
public void run()
{
while(true)
{
i++;
try
{
sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
if(i == 10)
break;
System.out.println("TestThead1 run() called!");
} }
} class TestThread2 extends Thread
{
int j;
public TestThread2()
{
j = 0;
} public void run()
{
while(true)
{
j++;
System.out.println("TestThead2 run() called!");
if(j == 10)
break;
}
}
} public class Test
{
public static void main(String[] args)
{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
//如何让t1先执行,但不是先执行完毕。!
t1.setPriority(6);
t2.setPriority(7); //在这里设定 优先级好像不行,大家看看我的运行结果,和不设置优先级是一样的!
t1.start();
t2.start();
}
}
=================结果
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
TestThead1 run() called!
package com.syj.Test;class TestThread1 extends Thread {
int i; public TestThread1() {
i = 0;
} public void run() {
while (true) {
i++;
try {
sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
if (i == 10)
break;
System.out.println("TestThead1 run() called!");
synchronized (Test.class) {
Test.class.notifyAll();
}
} }
}class TestThread2 extends Thread {
int j; public TestThread2() {
j = 0;
} public void run() {
synchronized (Test.class) {
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) {
j++;
System.out.println("TestThead2 run() called!");
if (j == 10)
break;
}
}
}public class Test {
public static void main(String[] args) {
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
// 如何让t1先执行,但不是先执行完毕。!
t1.start();
t2.start();
}
}
2 如果你非得强制1先运行,必须进行代码逻辑的控制,比如Thread1 里面加上
public static boolean running = false;在 run 里面加上
run(){
running = true;
}
然后再 Thread2 里面判断哪个running 是否为true;
run(){
while(!Treand1.runing){
try..
Thread.sleep(1);
catch..
}
}