在java中同步 [英] Synchronization in java
问题描述
class Thread1 extends 线程
{
Thread1( String s)
{
super (s);
start();
}
public synchronized void run()
{
for ( int i = 0 ; i< 4; i ++)
{
System.out.println(Thread.currentThread()。getName()+ 正在运行);
尝试
{
Thread.sleep( 1000 );
}
catch (例外e){}
}
}
}
类 Thread2 extends 线程
{
Thread2( String s)
{
super 跨度>(S);
start();
}
public void run()
{
sum();
}
public synchronized void sum()
{
for ( int i = 0 ; i< 4; i ++)
{
System.out.println(Thread.currentThread()。getName()+ 正在运行sum);
尝试
{
Thread.sleep( 1000 );
}
catch (例外e){}
}
}
}
class ThreadApply
{
public static void main(字符串 ... args)
{
Thread1 t1 = new Thread1( thread 1);
Thread2 t2 = new Thread2( thread 2\" 跨度>);
}
}
当我执行上述程序时,我得到了输出
线程1运行
线程2运行总和
线程1运行
线程2运行总和
线程1运行
线程2运行总额
线程1运行
线程2运行总额
虽然根据同步规则,同步方法中一次只执行一个线程,而不是像这样的输出...
Torsten - 添加一些代码块使这个可读
..因为另一个线程正在休眠。相反线程运行的机会。
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html [ ^ ]
这样可以更好地看到:
package com.cp;
public class ThreadApply {
public int iCount = 0;
public ThreadApply(){
Thread1 t1 = new Thread1 ( thread 1); // ++
Thread2 t2 = new Thread2( thread 2); // -
}
public static void main( String ... args){
new ThreadApply();
}
private class Thread1 extends 线程{
Thread1( String s){
超跨度>(S);
start();
}
public synchronized void run(){
for ( int i = < span class =code-digit> 0 ; i< 200 ; i ++){
iCount ++;
System.out.println(Thread.currentThread()。getName()+ iCount: + iCount);
尝试 {
// 了Thread.sleep(1000); //没有为你睡觉,先生Thread1
} catch (例外e){
}
}
}
}
私人 类 Thread2 extends 线程{
Thread2( String s){
超跨度>(S);
start();
}
public void run(){
sum();
}
public synchronized void sum(){
for ( int i = < span class =code-digit> 0 ; i< 20 ; i ++){
iCount--;
System.out.println( \t + Thread.currentThread()。 getName()+ iCount: + iCount);
尝试 {
Thread.sleep( 100 );
} catch (例外e){
}
}
}
}
}
Thread1
高速运行 -Thread2
正在等待Thread1
释放对引用值的锁定iCount
class Thread1 extends Thread
{
Thread1(String s)
{
super(s);
start();
}
public synchronized void run()
{
for(int i=0;i<4;i++)
{
System.out.println(Thread.currentThread().getName()+"is running");
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
}
}
class Thread2 extends Thread
{
Thread2(String s)
{
super(s);
start();
}
public void run()
{
sum();
}
public synchronized void sum()
{
for(int i=0;i<4;i++)
{
System.out.println(Thread.currentThread().getName()+"is running sum");
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
}
}
class ThreadApply
{
public static void main(String... args)
{
Thread1 t1 = new Thread1("thread 1");
Thread2 t2 = new Thread2("thread 2");
}
}
When i execute the above program i got the output
thread 1is running
thread 2is running sum
thread 1is running
thread 2is running sum
thread 1is running
thread 2is running sum
thread 1is running
thread 2is running sum
While according to the rule of synchonization only one thread is executed at a time in synchronized method than why the output like this...
Torsten - added some code blocks to make this readable
..because the other Thread is sleeping. That the chance for the opposite Thread to run.
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html[^]
It''s better visible like this:
package com.cp; public class ThreadApply { public int iCount=0; public ThreadApply(){ Thread1 t1 = new Thread1("thread 1"); // ++ Thread2 t2 = new Thread2("thread 2"); // -- } public static void main(String... args) { new ThreadApply(); } private class Thread1 extends Thread { Thread1(String s) { super(s); start(); } public synchronized void run() { for (int i = 0; i < 200; i++) { iCount++; System.out.println(Thread.currentThread().getName() + " iCount:" + iCount); try { // Thread.sleep(1000); // no sleeping for you, Mr. Thread1 } catch (Exception e) { } } } } private class Thread2 extends Thread { Thread2(String s) { super(s); start(); } public void run() { sum(); } public synchronized void sum() { for (int i = 0; i < 20; i++) { iCount--; System.out.println("\t" + Thread.currentThread().getName() + " iCount:" + iCount); try { Thread.sleep(100); } catch (Exception e) { } } } } }
Thread1
is running at high speed -Thread2
is waiting untilThread1
has releases the lock on the referenced valueiCount
.
这篇关于在java中同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!