下午闲来无事,复习了一下线程的知识,可是出现了与我想不一样的结果,不知怎么回事public class ThreadUse {
public static void main(String[] args) {
StoreObject myStoreObject=new StoreObject();
ZyiyiThread1 myThread1=new ZyiyiThread1(myStoreObject);
ZyiyiThread2 myThread2=new ZyiyiThread2(myStoreObject);
myThread1.setPriority(4);
myThread2.setPriority(4);
myThread1.start();
myThread2.start();
}
}class StoreObject{
private static int treasure=0;
public synchronized void storeIn(){
++treasure;
System.out.println("storeIn,now treasure: "+treasure);
}
public synchronized void storeOut(){
--treasure;
System.out.println("storeOut,now treasure: "+treasure);
}
}class ZyiyiThread1 extends Thread{
private StoreObject myStoreObject1=new StoreObject();
public ZyiyiThread1(StoreObject myStoreObject){
myStoreObject1=myStoreObject;
}
public void run(){
System.out.println("zyi1 runing");
for(int i=0;i<10;i++){
try{
Thread.sleep((int) (Math.random()*100));
}catch(InterruptedException e){}
myStoreObject1.storeIn();
}
}
}
/*class RunnableThread2 implements Runnable{ public void run() {
System.out.println("zyi2 runing");
}
}*/
class ZyiyiThread2 extends Thread{
private StoreObject myStoreObject2=new StoreObject();
public ZyiyiThread2(StoreObject myStoreObject){
myStoreObject2=myStoreObject;
}
public void run(){
System.out.println("zyi2 runing");
for(int i=0;i<10;i++){
myStoreObject2.storeOut();
try{
Thread.sleep((int) (Math.random()*1000));
}catch(InterruptedException e){}
}
}
}结果是:
zyi1 runing
zyi2 runing
storeOut,now treasure: -1
storeIn,now treasure: 0
storeIn,now treasure: 1
storeIn,now treasure: 2
storeIn,now treasure: 3
storeIn,now treasure: 4
storeIn,now treasure: 5
storeIn,now treasure: 6
storeIn,now treasure: 7
storeIn,now treasure: 8
storeIn,now treasure: 9
storeOut,now treasure: 8
storeOut,now treasure: 7
storeOut,now treasure: 6
storeOut,now treasure: 5
storeOut,now treasure: 4
storeOut,now treasure: 3
storeOut,now treasure: 2
storeOut,now treasure: 1
storeOut,now treasure: 0本应该是:
zyi1 runing
storeIn,now treasure: 1
zyi2 runing
storeOut,now treasure:0
...
不知道那里有问题了,希望大家回答,这是最基本的问题,谢谢了
public static void main(String[] args) {
StoreObject myStoreObject=new StoreObject();
ZyiyiThread1 myThread1=new ZyiyiThread1(myStoreObject);
ZyiyiThread2 myThread2=new ZyiyiThread2(myStoreObject);
myThread1.setPriority(4);
myThread2.setPriority(4);
myThread1.start();
myThread2.start();
}
}class StoreObject{
private static int treasure=0;
public synchronized void storeIn(){
++treasure;
System.out.println("storeIn,now treasure: "+treasure);
}
public synchronized void storeOut(){
--treasure;
System.out.println("storeOut,now treasure: "+treasure);
}
}class ZyiyiThread1 extends Thread{
private StoreObject myStoreObject1=new StoreObject();
public ZyiyiThread1(StoreObject myStoreObject){
myStoreObject1=myStoreObject;
}
public void run(){
System.out.println("zyi1 runing");
for(int i=0;i<10;i++){
try{
Thread.sleep((int) (Math.random()*100));
}catch(InterruptedException e){}
myStoreObject1.storeIn();
}
}
}
/*class RunnableThread2 implements Runnable{ public void run() {
System.out.println("zyi2 runing");
}
}*/
class ZyiyiThread2 extends Thread{
private StoreObject myStoreObject2=new StoreObject();
public ZyiyiThread2(StoreObject myStoreObject){
myStoreObject2=myStoreObject;
}
public void run(){
System.out.println("zyi2 runing");
for(int i=0;i<10;i++){
myStoreObject2.storeOut();
try{
Thread.sleep((int) (Math.random()*1000));
}catch(InterruptedException e){}
}
}
}结果是:
zyi1 runing
zyi2 runing
storeOut,now treasure: -1
storeIn,now treasure: 0
storeIn,now treasure: 1
storeIn,now treasure: 2
storeIn,now treasure: 3
storeIn,now treasure: 4
storeIn,now treasure: 5
storeIn,now treasure: 6
storeIn,now treasure: 7
storeIn,now treasure: 8
storeIn,now treasure: 9
storeOut,now treasure: 8
storeOut,now treasure: 7
storeOut,now treasure: 6
storeOut,now treasure: 5
storeOut,now treasure: 4
storeOut,now treasure: 3
storeOut,now treasure: 2
storeOut,now treasure: 1
storeOut,now treasure: 0本应该是:
zyi1 runing
storeIn,now treasure: 1
zyi2 runing
storeOut,now treasure:0
...
不知道那里有问题了,希望大家回答,这是最基本的问题,谢谢了
for(int i=0;i<10;i++){
try{
Thread.sleep((int) (Math.random()*100));
}catch(InterruptedException e){}
myStoreObject1.storeIn();
}
}
首先这样的代码,是10次循环,可以解释为啥有那么多输出语句。其次,你的代码只是保证了treasure的同步问题,但并不能保证两个线程的执行顺序。
你这线程的锁只能保证每次treasure变化后 能正确输出当前的值
并不能保证两个线程的执行顺序
public static void main(String[] args) {
StoreObject myStoreObject = new StoreObject();
ZyiyiThread1 myThread1 = new ZyiyiThread1(myStoreObject);
ZyiyiThread2 myThread2 = new ZyiyiThread2(myStoreObject);
myThread1.setPriority(4);
myThread2.setPriority(4);
myThread1.start();
myThread2.start();
}
}class StoreObject {
private static int treasure = 0; public synchronized void storeIn() {
++treasure;
System.out.println("storeIn,now treasure: " + treasure);
} public synchronized void storeOut() {
--treasure;
System.out.println("storeOut,now treasure: " + treasure);
}
}class ZyiyiThread1 extends Thread {
private StoreObject myStoreObject1 = new StoreObject(); public ZyiyiThread1(StoreObject myStoreObject) {
myStoreObject1 = myStoreObject;
} public void run() { for (int i = 0; i < 10; i++) {
System.out.println("zyi1 runing");
try {
Thread.sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
}
myStoreObject1.storeIn();
}
}
}class ZyiyiThread2 extends Thread {
private StoreObject myStoreObject2 = new StoreObject(); public ZyiyiThread2(StoreObject myStoreObject) {
myStoreObject2 = myStoreObject;
} public void run() { for (int i = 0; i < 10; i++) {
System.out.println("zyi2 runing");
myStoreObject2.storeOut();
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
}
}
是不是这样的代码
Thread t2 = new Thread(new YourThread());t1.start();
t2.start();试试
private StoreObject myStoreObject1 = new StoreObject();
改成
private StoreObject myStoreObject1;试试