【多线程】synchronized同步方法
2012-07-30 16:21
411 查看
public class SynchronizedTest2 { public static void main(String[] args) { ThreadTest t1 = new ThreadTest(); new Thread(t1).start(); new Thread(t1).start(); System.out.println(t1.call()); } } class ThreadTest implements Runnable{ private int x; private int y; @Override public synchronized void run() { for(int i=0;i<4;i++){ x++; y++; try { Thread.sleep(200); } catch (InterruptedException e) { System.out.println("线程出错了!!!"); } System.out.println(Thread.currentThread().getName() + " x==" + x + ",y==" + y + " " + i); } } public synchronized String call(){ String name = Thread.currentThread().getName(); return "hellow " + name; } }
相关文章推荐
- java多线程之-----静态同步synchronized方法与synchronized(class) 代码块
- java多线程编程之使用Synchronized关键字同步类方法
- java 多线程10:synchronized锁机制 之 锁定类静态方法 和锁定类.Class 和 数据String的常量池特性 同步静态方法
- Java多线程之同步锁定--synchronized同步方法和同步块、脏读、锁重入、死琐
- Java:多线程,线程同步,synchronized关键字的用法(同步代码块、非静态同步方法、静态同步方法)
- 初学Java多线程:使用Synchronized关键字同步类方法
- 多线程数据同步之synchronized同步方法-多线程访问临界资源
- 多线程:synchronized同步方法和同步代码快的区别
- 多线程中的同步之synchronized应用——synchronized方法
- 初学Java多线程:使用Synchronized关键字同步类方法
- 从头认识多线程-2.25 synchronized同步方法在jvm是怎样执行的?
- 多线程:synchronized同步方法
- Java多线程3.1:synchronized同步方法
- 多线程--synchronized同步方法
- 【转载】Java多线程编程2--同步锁定--synchronized同步方法、脏读、锁重入
- 【Java面试题】26 多线程有几种实现方法?同步有几种实现方法? 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法?
- 从头认识多线程-2.14 解决由同步的synchronized (newobject()) 引起的脏读的方法
- 从头认识多线程-2.19 synchronized同步方法的无限等待与解决方法
- 四、java多线程核心技术——synchronized同步方法与synchronized同步快
- JavaSE8基础 多线程synchronized 同步的成员方法用的锁对象是this