package study;public class ThreadDemo{
public static void main(String args[]){
ThreadTest t = new ThreadTest();
new Thread(t).start(); try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
} t.str = new String("method");
new Thread(t).start();
}
}class ThreadTest implements Runnable{
private int tickets = 10;
String str = new String();
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
synchronized(str){
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}
}
public synchronized void sale(){
if(tickets > 0){
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.print("test: ");
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}
}结果怎么变到这样了??Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
test: Thread-1 is saleing ticket 8
test: Thread-1 is saleing ticket 7
test: Thread-1 is saleing ticket 6
test: Thread-1 is saleing ticket 5
test: Thread-1 is saleing ticket 4
test: Thread-1 is saleing ticket 3
test: Thread-1 is saleing ticket 2
test: Thread-1 is saleing ticket 1
public static void main(String args[]){
ThreadTest t = new ThreadTest();
new Thread(t).start(); try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
} t.str = new String("method");
new Thread(t).start();
}
}class ThreadTest implements Runnable{
private int tickets = 10;
String str = new String();
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
synchronized(str){
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}
}
public synchronized void sale(){
if(tickets > 0){
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.print("test: ");
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}
}结果怎么变到这样了??Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
test: Thread-1 is saleing ticket 8
test: Thread-1 is saleing ticket 7
test: Thread-1 is saleing ticket 6
test: Thread-1 is saleing ticket 5
test: Thread-1 is saleing ticket 4
test: Thread-1 is saleing ticket 3
test: Thread-1 is saleing ticket 2
test: Thread-1 is saleing ticket 1
synchronized一个方法=synchronized这个方法的调用者也就是this
就结束了生命期 thread-1与前线程共享了一个target 进入sale同步方法显示9项后无所作为
但程序因为while一直阻塞着 此例中没有出现互斥场合
哪位高人帮忙给个正确code吧.谢谢.
target应该是同一个 用this可以使两者同步的
只不过lz这种代码编排方式无法显现明显效果
将两者的sleep语句都放在后面 且加大延时数
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
synchronized(this){
if(tickets > 0){
try{
Thread.sleep(10);
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}
} 哪位高手帮看下,能给个code最好了,谢谢了.
所以synchronized方法肯定不可以...需要synchronized同一个对象
public static void main(String args[]){
ThreadTest t = new ThreadTest();
new Thread(t).start();
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
} t.str = new String("method");
new Thread(t).start();
}
}class ThreadTest implements Runnable{
private int tickets = 10;
String str = new String();
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
synchronized(this){
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
try{
Thread.sleep(3000);
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
public synchronized void sale(){
if(tickets > 0){
System.out.print("test: ");
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
try{
Thread.sleep(3000);
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}如有问题 请指出
Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
test: Thread-1 is saleing ticket 8
test: Thread-1 is saleing ticket 7
test: Thread-1 is saleing ticket 6
test: Thread-1 is saleing ticket 5
test: Thread-1 is saleing ticket 4
test: Thread-1 is saleing ticket 3
test: Thread-1 is saleing ticket 2
test: Thread-1 is saleing ticket 1
Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
只要以上两句隔3秒执行就算互斥了
Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
Thread-0 is saleing ticket 8
test: Thread-1 is saleing ticket 7
Thread-0 is saleing ticket 6
test: Thread-1 is saleing ticket 5
Thread-0 is saleing ticket 4
test: Thread-1 is saleing ticket 3
Thread-0 is saleing ticket 2
test: Thread-1 is saleing ticket 1
public static void main(String args[]){
ThreadTest t = new ThreadTest();
new Thread(t).start();
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
} t.str = new String("method");
new Thread(t).start();
}
}class ThreadTest implements Runnable{
private int tickets = 10;
String str = new String();
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
while (true) {
synchronized (this) {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName()
+ " is saleing ticket " + tickets--);
try {
Thread.sleep(100);
this.wait();
this.notify();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
}
}
public synchronized void sale(){
if(tickets > 0){
System.out.print("test: ");
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
try{
Thread.sleep(100);
this.notify();
this.wait();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
result:
Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
Thread-0 is saleing ticket 8
test: Thread-1 is saleing ticket 7
Thread-0 is saleing ticket 6
test: Thread-1 is saleing ticket 5
Thread-0 is saleing ticket 4
test: Thread-1 is saleing ticket 3
Thread-0 is saleing ticket 2
test: Thread-1 is saleing ticket 1