//不加同步控制
public class ThreadDemo1 {
public static void main(String[] args) {
TestThread tt = new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TestThread implements Runnable{
private int tickets = 100;
public void run(){
while(true){
if(tickets>0){//如果thread1执行到这被thread2抢了时间片就有了同步问题
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is sailing ticket " + tickets--);
}
}
}
}
//result
//Thread-0 is sailing ticket 5
//Thread-3 is sailing ticket 4
//Thread-2 is sailing ticket 3
//Thread-4 is sailing ticket 2
//Thread-1 is sailing ticket 1
//Thread-2 is sailing ticket 0
//Thread-3 is sailing ticket -1
//Thread-0 is sailing ticket -2
//Thread-4 is sailing ticket –3加同步控制
package myk;
public class ThreadDemo1 {
public static void main(String[] args) {
TestThread tt = new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TestThread implements Runnable{
private int tickets = 20;
String str = new String("");
public void run(){//String str = new String(""); 如果把标志位放这则起不到同步的效果 while(true){
synchronized (str) {//标志位对象,该标志位具有0,1两种状态,当有线程执行这块代码时,就把标志位置为0,执行完后就把标志为置为1这个标志叫锁旗标,一个用于synchronized语句中的对象称为一个监视器,当一个线程获得了synchronized(object)语句中的代码块的执行权,意味着锁定了监视器同步处理后,程序运行速度会减慢,因为系统不停的对同步监视器进行检查
if(tickets>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is sailing ticket " + tickets--);
}
}
}
}
}
//result
//Thread-0 is sailing ticket 20
//Thread-1 is sailing ticket 19
//Thread-2 is sailing ticket 18
//Thread-3 is sailing ticket 17
//Thread-4 is sailing ticket 16
//Thread-0 is sailing ticket 15
//Thread-1 is sailing ticket 14
//Thread-2 is sailing ticket 13
//Thread-3 is sailing ticket 12
//Thread-4 is sailing ticket 11
//Thread-0 is sailing ticket 10
//Thread-1 is sailing ticket 9
//Thread-2 is sailing ticket 8
//Thread-3 is sailing ticket 7
//Thread-4 is sailing ticket 6
//Thread-0 is sailing ticket 5
//Thread-1 is sailing ticket 4
//Thread-2 is sailing ticket 3
//Thread-3 is sailing ticket 2
//Thread-4 is sailing ticket 1同步函数
public class ThreadDemo1 {
public static void main(String[] args) {
TestThread tt = new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TestThread implements Runnable{
private int tickets = 20;
public void run(){
while(true){
sale();
}
}
public synchronized void sale(){
if(tickets>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is sailing ticket " + tickets--);
}
}
}代码块与函数间的同步
public class ThreadDemo1 {
public static void main(String[] args) {
TestThread t = new TestThread();
new Thread(t).start();//调用同步方法 注意这块,启动了线程,不一定马上就执行这个线程
try {
Thread.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.str = new String("method");
new Thread(t).start();//这个线程调用同步代码块
}
}
class TestThread implements Runnable{
private int tickets = 100;
String str = new String("");
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
synchronized (str) {
try {
Thread.sleep(100);//通过Thread.interrupt(),可以打断线程的睡眠,//当然一个线程调用另一个线程的inerrupt,线程的睡眠被打断后进入Runnable状态。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is sailing ticket " + tickets--);
}
}
}
public synchronized void sale(){//同步函数使用的是this监视器
if(tickets>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("test:");
System.out.println(Thread.currentThread().getName()+" is sailing ticket " + tickets--);
}
}
}
给你举了2 个例子, 你运行下好好研究下就明白了。 |