Java¶àÏ̱߳à³ÌÖеÄSynchronized
Synchronized£¬¹ËÃû˼Ò壬´ú±í¶à¸öÏß³ÌÖ®¼äµÄͬ²½¡£ÄÇô£¬ÔÚjava±à³ÌÖÐÈçºÎʹÓÃËüÄØ£¿ ÎÒÃǼÙÉèÕâÑùÒ»¸öÇé¿ö£º¶ÔÓÚͬһ¸ö±äÁ¿synDemo£¬ÎÒÃÇ·Ö±ðÔÚ2¸ö²»Í¬µÄÏß³ÌÖе÷ÓÃsynDemo.synMethord1()ÓësynDemo.synMethord2()¡£ package com.cnblogs.gpcuster;
/**
*
* @author Aaron.Guo
*
*/
public class Tester {
public static void main(String[] args) {
final SynDemo synDemo = new SynDemo();
Thread thread1 = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
synDemo.synMethord1();
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
synDemo.synMethord2();
}
};
thread1.start();
thread2.start();
while (true) {
try {
Thread.sleep(1000);
System.out.println("main");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ÎÒÃǵÄSynDemoÊÇÕâÑù¶¨ÒåµÄ£º
package com.cnblogs.gpcuster;
/**
*
* @author Aaron.Guo
*
*/
public class SynDemo{
public void synMethord1() {
while(true) {
try {
Thread.sleep(1000);
System.out.println("synMethord1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void synMethord2() {
while(true) {
try {
Thread.sleep(1000);
System.out.println("synMethord2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ÎÒÃǵÄSynDemo¶ÔÏóûÓÐÈκÎÌØÊâµÄ¶¨Ò壬ËùÒÔÔËÐÐÆðÀ´ÒÔºóµÄÇé¿öÈçÏ£º
synMethord2
synMethord1
main
synMethord2
synMethord1
main
synMethord2
synMethord1
½ÓÏÂÀ´£¬ÎÒÃǸøsynMethord1Ìí¼ÓÉÏsynchronizedÉùÃ÷£¬ÔËÐÐÇé¿ö»¹ÊÇÓëÉÏ´ÎÒ»Ñù£¬ÒòΪÎÒÃÇÖ»ÓÐÒ»¸öÏß³ÌÔÚµ÷ÓÃsynMethord1µÄ·½·¨¡£
ÎÒÃǸøsynMethord2Ò²Ìí¼ÓÉÏsynchronizedÉùÃ÷£¬SynDemo¶ÔÏóµÄ´úÂë
Ïà¹ØÎĵµ£º
ÎÒÃÇÿ¸öѧÆÚÄ©¶¼»á½øÐн«½üÒ»¸öÔµÄʵѵ£¬Õâ´ÎJavaʵѵҲ¼´½«½áÊø£¬ÎÒÑ¡µÄ¿ÎÌâÊÇ“¸öÈËÍøÉÏÒøÐÐϵͳ”£¬ÔËÓÃÁËStruts£¬HibernateºÍSpring¿ò¼Ü£¬Í¨¹ýmvcÉè¼Æģʽ¡£»ù±¾ËùÓеŦÄܶ¼ÊµÏÖÁË£¬ÔÚʵѵÖÐÕæµÄѧµ½Á˺ܶà֪ʶ£¬Ö®¼äÒ²Óöµ½Á˺ܶàÎÊÌ⣬µ«¶¼Ò»Ò»µÃµ½½â¾ö¡£ÎÒ½øÒ»²½Á˽âÁËStrutsµÄÔËÐÐÔÀí£¬ÂíÉÏÃæÁÙ ......
½ñÌìͬѧÎÊÎÒÒ»¸öJavaÎÊÌ⣬´úÂë½á¹¹ÈçÏ£º
Scanner scan = new Scanner(System.in);
scan.next();
scan.close();
scan = new Scanner(System.in);
scan.next();
µ±³ÌÐòÔËÐе½µÚÎåÐÐÊÇ»áÅ׳öjava.util.NoSuchElementException
at&nb ......
³£¼ûµÄJAVA³ÌÐò°üÀ¨ÒÔÏÂÀà±ð£º
*Ö±½ÓÔÚconsoleÉÏÔËÐеÄÀà(°üÀ¨¿ÉÊÓ»¯½çÃæµÄÀà)
*JSP´úÂëÀࣨע£ºJSPÊÇServletsÀàµÄ±äÐÍ£©
*ServeletsÀà
*EJBÀà
*ÆäËü²»¿ÉÒÔÖ±½ÓÔËÐеÄÖ§³ÖÀà
ÕâЩÀàÎļþÖУ¬¶¼ÓпÉÄܺ¬ÓÐÖÐÎÄ×Ö·û´®£¬²¢ÇÒ³£ÓÃÇ°ÈýÀàJAVA³ÌÐòºÍÓû§Ö±½Ó½»»¥£¬ÓÃÓÚÊä³öºÍÊäÈë×Ö·û£¬È磺ÔÚJSPºÍServletÖеõ½¿Í»§ ......
1.±à³Ì½ç×îÖøÃûµÄÒ»¾ä»°“Hello world!”,ÈκÎÒ»¸ö³ÌÐòÔ±Æð²½Ê±±ØÓöµÄÓï¾ä
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Hello World!");
......
¶þ·Ö²éÕÒ£º
¶þ·Ö²éÕÒ±ÈÏßÐÔ²éÕÒÊý¶ÈÒªºÃºÜ¶à£¬ÓÈÆäÊǶԴóÊý×éÀ´Ëµ¸üΪÏÔÖø£¬¾ÍÏñÊDzÂ×ÖÓÎÏ·Ò»Ñù
²Â×ÖÓÎÏ·£º
¶þ·Ö²éÕҵķ½·¨ºÍÎÒÃÇСʱºòÍæµÄ²Â×ÖÓÎÏ·ÊÇÒ»ÑùµÄ¡£ÔÚÕâ¸öÓÎÏ·ÀïÃ棬ÓÐÒ»¸öСÅóÓÑ»áÈÃÄã²ÂËýÕýÔÚÏëµÄÒ»¸ö1ÖÁ100Ö®¼äµÄÊý£¬
Äã²ÂÁËÒ»¸öÊýÒÔºó£¬Ëý»á¸æËßÄãÈýÖÖÑ¡ÔñÖеÄÒ»¸ö£ºÄã² ......