ÒÔǰûÓÐд±Ê¼ÇµÄϰ¹ß£¬ÏÖÔÚÂýÂýµÄ·¢ÏÖ¼°Ê±×ܽáÊǶàôµÄÖØÒªÁË£¬ºÇºÇ¡£ËäÈ»²Å´ó¶þ£¬µ«ÊÇÒ²¿ìÒª±ÏÒµÁË£¬Òª¼ÓÓÍÁË¡£
ÕâһƪÎÄÕÂÖ÷Òª¹ØÓÚjava¶àỊ̈߳¬Ö÷Òª»¹ÊÇÒÔÀý×ÓÀ´Çý¶¯µÄ¡£ÒòΪ½²½â¶àÏ̵߳ÄÊé¼®ºÍÎÄÕÂÒѾºÜ¶àÁË£¬ËùÒÔÎÒÒ²²»ºÃÒâ˼¶à˵£¬ºÇºÇ¡¢´ó¼Ò¿ÉÒÔÈ¥²Î¿¼Ò»Ð©ÄÇЩÊé¼®¡£ÎÒÕâ¸öÎÄÕÂÖ÷Òª¹ØÓÚʵ¼ÊµÄһЩÎÊÌ⡣ͬʱҲËãÊÇÎÒÒÔºó¸´Ï°µÄ×ÊÁϰɣ¬¡£ºÇºÇ´ó¼Ò¶à¶àÖ¸½Ì¡£
ͬʱϣÍû¶à½á½»Ò»Ð©¼¼ÊõÉϵÄÅóÓÑ¡£Ð»Ð»¡£
javaÖеĶàÏß³Ì
ÔÚjavaÖÐÒªÏëʵÏÖ¶àỊ̈߳¬ÓÐÁ½ÖÖÊֶΣ¬Ò»ÖÖÊǼÌÐøThreadÀ࣬ÁíÍâÒ»ÖÖÊÇʵÏÖRunable½Ó¿Ú¡£
¶ÔÓÚÖ±½Ó¼Ì³ÐThreadµÄÀàÀ´Ëµ£¬´úÂë´óÖ¿ò¼ÜÊÇ£º
class ˈ̞ extends Thread{
·½·¨1;
·½·¨2£»
¡
public void run(){
// other code¡
}
ÊôÐÔ1£»
ÊôÐÔ2£»
¡
} |
ÏÈ¿´Ò»¸ö¼òµ¥µÄÀý×Ó£º
/**
* @author Rollen-Holt ¼Ì³ÐThreadÀà,Ö±½Óµ÷ÓÃrun·½·¨
* */
class hello extends Thread {
public hello() {
}
public hello(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(name + "ÔËÐÐ " +
i);
}
}
public static void main(String[] args) {
hello h1=new hello("A");
hello h2=new hello("B");
h1.run();
h2.run();
}
private String name;
} |
¡¾ÔËÐнá¹û¡¿£º
AÔËÐÐ 0
AÔËÐÐ 1
AÔËÐÐ 2
AÔËÐÐ 3
AÔËÐÐ 4
BÔËÐÐ 0
BÔËÐÐ 1
BÔËÐÐ 2
BÔËÐÐ 3
BÔËÐÐ 4
ÎÒÃǻᷢÏÖÕâЩ¶¼ÊÇ˳ÐòÖ´Ðеģ¬ËµÃ÷ÎÒÃǵĵ÷Ó÷½·¨²»¶Ô£¬Ó¦¸Ãµ÷ÓõÄÊÇstart£¨£©·½·¨¡£
µ±ÎÒÃǰÑÉÏÃæµÄÖ÷º¯ÊýÐÞ¸ÄΪÈçÏÂËùʾµÄʱºò£º
public static void main(String[] args) {
hello h1=new hello("A");
hello h2=new hello("B");
h1.start();
h2.start();
} |
È»ºóÔËÐгÌÐò£¬Êä³öµÄ¿ÉÄܵĽá¹ûÈçÏ£º
AÔËÐÐ 0
BÔËÐÐ 0
BÔËÐÐ 1
BÔËÐÐ 2
BÔËÐÐ 3
BÔËÐÐ 4
AÔËÐÐ 1
AÔËÐÐ 2
AÔËÐÐ 3
AÔËÐÐ 4
ÒòΪÐèÒªÓõ½CPUµÄ×ÊÔ´£¬ËùÒÔÿ´ÎµÄÔËÐнá¹û»ù±¾ÊǶ¼²»Ò»ÑùµÄ£¬ºÇºÇ¡£
×¢Ò⣺ËäÈ»ÎÒÃÇÔÚÕâÀïµ÷ÓõÄÊÇstart£¨£©·½·¨£¬µ«ÊÇʵ¼ÊÉϵ÷ÓõϹÊÇrun£¨£©·½·¨µÄÖ÷Ìå¡£
ÄÇô£ºÎªÊ²Ã´ÎÒÃDz»ÄÜÖ±½Óµ÷ÓÃrun£¨£©·½·¨ÄØ£¿
ÎÒµÄÀí½âÊÇ£ºÏ̵߳ÄÔËÐÐÐèÒª±¾µØ²Ù×÷ϵͳµÄÖ§³Ö¡£
Èç¹ûÄã²é¿´startµÄÔ´´úÂëµÄʱºò£¬»á·¢ÏÖ£º
public synchronized void start() {
/**
* This method is not invoked for the main method
thread or "system"
* group threads created/set up by the VM. Any
new functionality added
* to this method in the future may have to also
be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0 || this != me)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}
private native void start0(); |
×¢ÒâÎÒÓúìÉ«¼Ó´ÖµÄÄÇÒ»ÌõÓï¾ä£¬ËµÃ÷´Ë´¦µ÷ÓõÄÊÇstart0£¨£©¡£²¢ÇÒÕâ¸öÕâ¸ö·½·¨ÓÃÁËnative¹Ø¼ü×Ö£¬´Î¹Ø¼ü×Ö±íʾµ÷Óñ¾µØ²Ù×÷ϵͳµÄº¯Êý¡£ÒòΪ¶àÏ̵߳ÄʵÏÖÐèÒª±¾µØ²Ù×÷ϵͳµÄÖ§³Ö¡£
µ«ÊÇstart·½·¨Öظ´µ÷Óõϰ£¬»á³öÏÖjava.lang.IllegalThreadStateExceptionÒì³£¡£
ͨ¹ýʵÏÖRunnable½Ó¿Ú£º
´óÖ¿ò¼ÜÊÇ£º
class ˈ̞ implements Runnable{
·½·¨1;
·½·¨2£»
¡
public void run(){
// other code¡
}
ÊôÐÔ1£»
ÊôÐÔ2£»
¡
} |
À´ÏÈ¿´Ò»¸öСÀý×Ó°É£º
/**
* @author Rollen-Holt ʵÏÖRunnable½Ó¿Ú
* */
class hello implements Runnable {
public hello() {
}
public hello(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(name + "ÔËÐÐ " +
i);
}
}
public static void main(String[] args) {
hello h1=new hello("Ïß³ÌA");
Thread demo= new Thread(h1);
hello h2=new hello("Ị̈߳Â");
Thread demo1=new Thread(h2);
demo.start();
demo1.start();
}
private String name;
} |
¡¾¿ÉÄܵÄÔËÐнá¹û¡¿£º
Ïß³ÌAÔËÐÐ 0
Ị̈߳ÂÔËÐÐ 0
Ị̈߳ÂÔËÐÐ 1
Ị̈߳ÂÔËÐÐ 2
Ị̈߳ÂÔËÐÐ 3
Ị̈߳ÂÔËÐÐ 4
Ïß³ÌAÔËÐÐ 1
Ïß³ÌAÔËÐÐ 2
Ïß³ÌAÔËÐÐ 3
Ïß³ÌAÔËÐÐ 4
¹ØÓÚÑ¡Ôñ¼Ì³ÐThread»¹ÊÇʵÏÖRunnable½Ó¿Ú£¿
ÆäʵThreadÒ²ÊÇʵÏÖRunnable½Ó¿ÚµÄ£º
class Thread implements Runnable {
//¡
public void run() {
if (target != null) {
target.run();
}
}
} |
ÆäʵThreadÖеÄrun·½·¨µ÷ÓõÄÊÇRunnable½Ó¿ÚµÄrun·½·¨¡£²»ÖªµÀ´ó¼Ò·¢ÏÖûÓУ¬ThreadºÍRunnable¶¼ÊµÏÖÁËrun·½·¨£¬ÕâÖÖ²Ù×÷ģʽÆäʵ¾ÍÊÇ´úÀíģʽ¡£¹ØÓÚ´úÀíģʽ£¬ÎÒÔø¾Ð´¹ýÒ»¸öСÀý×ӺǺǣ¬´ó¼ÒÓÐÐËȤµÄ»°¿ÉÒÔ¿´Ò»Ï£ºhttp://www.cnblogs.com/rollenholt/archive/2011/08/18/2144847.html
ThreadºÍRunnableµÄÇø±ð£º
Èç¹ûÒ»¸öÀà¼Ì³ÐThread£¬Ôò²»ÊʺÏ×ÊÔ´¹²Ïí¡£µ«ÊÇÈç¹ûʵÏÖÁËRunable½Ó¿ÚµÄ»°£¬ÔòºÜÈÝÒ×µÄʵÏÖ×ÊÔ´¹²Ïí¡£
/**
* @author Rollen-Holt ¼Ì³ÐThreadÀ࣬²»ÄÜ×ÊÔ´¹²Ïí
* */
class hello extends Thread {
public void run() {
for (int i = 0; i < 7; i++) {
if (count > 0) {
System.out.println("count= " + count--);
}
}
}
public static void main(String[] args) {
hello h1 = new hello();
hello h2 = new hello();
hello h3 = new hello();
h1.start();
h2.start();
h3.start();
}
private int count = 5;
} |
¡¾ÔËÐнá¹û¡¿£º
count= 5
count= 4
count= 3
count= 2
count= 1
count= 5
count= 4
count= 3
count= 2
count= 1
count= 5
count= 4
count= 3
count= 2
count= 1
´ó¼Ò¿ÉÒÔÏëÏó£¬Èç¹ûÕâ¸öÊÇÒ»¸öÂòƱϵͳµÄ»°£¬Èç¹ûcount±íʾµÄÊdzµÆ±µÄÊýÁ¿µÄ»°£¬ËµÃ÷²¢Ã»ÓÐʵÏÖ×ÊÔ´µÄ¹²Ïí¡£
ÎÒÃÇ»»ÎªRunnable½Ó¿Ú
class MyThread implements Runnable{
private int ticket = 5; //5ÕÅÆ±
public void run() {
for (int i=0; i<=20; i++) {
if (this.ticket > 0) {
System.out.println(Thread.currentThread().getName()+
"ÕýÔÚÂôƱ"+this.ticket--);
}
}
}
}
public class lzwCode {
public static void main(String [] args) {
MyThread my = new MyThread();
new Thread(my, "1ºÅ´°¿Ú").start();
new Thread(my, "2ºÅ´°¿Ú").start();
new Thread(my, "3ºÅ´°¿Ú").start();
}
} |

¡¾ÔËÐнá¹û¡¿£º
count= 5
count= 4
count= 3
count= 2
count= 1
×ܽáһϰɣº
ʵÏÖRunnable½Ó¿Ú±È¼Ì³ÐThreadÀàËù¾ßÓеÄÓÅÊÆ£º
1£©£ºÊʺ϶à¸öÏàͬµÄ³ÌÐò´úÂëµÄÏß³ÌÈ¥´¦Àíͬһ¸ö×ÊÔ´
2£©£º¿ÉÒÔ±ÜÃâjavaÖеĵ¥¼Ì³ÐµÄÏÞÖÆ
3£©£ºÔö¼Ó³ÌÐòµÄ½¡×³ÐÔ£¬´úÂë¿ÉÒÔ±»¶à¸öÏ̹߳²Ïí£¬´úÂëºÍÊý¾Ý¶ÀÁ¢¡£
ËùÒÔ£¬±¾È˽¨Òé´ó¼Ò¾¢Á¿ÊµÏÖ½Ó¿Ú¡£
/**
* @author Rollen-Holt
* È¡µÃÏ̵߳ÄÃû³Æ
* */
class hello implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String[] args) {
hello he = new hello();
new Thread(he,"A").start();
new Thread(he,"B").start();
new Thread(he).start();
}
} |
¡¾ÔËÐнá¹û¡¿£º
A
A
A
B
B
B
Thread-0
Thread-0
Thread-0
˵Ã÷Èç¹ûÎÒÃÇûÓÐÖ¸¶¨Ãû×ֵϰ£¬ÏµÍ³×Ô¶¯ÌṩÃû×Ö¡£
ÌáÐÑһϴó¼Ò£ºmain·½·¨ÆäʵҲÊÇÒ»¸öÏ̡߳£ÔÚjavaÖÐËùÒÔµÄÏ̶߳¼ÊÇͬʱÆô¶¯µÄ£¬ÖÁÓÚʲôʱºò£¬ÄĸöÏÈÖ´ÐУ¬ÍêÈ«¿´ËÏȵõ½CPUµÄ×ÊÔ´¡£
ÔÚjavaÖУ¬Ã¿´Î³ÌÐòÔËÐÐÖÁÉÙÆô¶¯2¸öÏ̡߳£Ò»¸öÊÇmainỊ̈߳¬Ò»¸öÊÇÀ¬»øÊÕ¼¯Ï̡߳£ÒòΪÿµ±Ê¹ÓÃjavaÃüÁîÖ´ÐÐÒ»¸öÀàµÄʱºò£¬Êµ¼ÊÉ϶¼»áÆô¶¯Ò»¸ö£Ê£Ö£Í£¬Ã¿Ò»¸ö£ê£Ö£ÍʵϰÔÚ¾ÍÊÇÔÚ²Ù×÷ϵͳÖÐÆô¶¯ÁËÒ»¸ö½ø³Ì¡£
ÅжÏÏß³ÌÊÇ·ñÆô¶¯
/**
* @author Rollen-Holt ÅжÏÏß³ÌÊÇ·ñÆô¶¯
* */
class hello implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String[] args) {
hello he = new hello();
Thread demo = new Thread(he);
System.out.println("Ïß³ÌÆô¶¯Ö®Ç°---¡·" +
demo.isAlive());
demo.start();
System.out.println("Ïß³ÌÆô¶¯Ö®ºó---¡·" +
demo.isAlive());
}
} |
¡¾ÔËÐнá¹û¡¿
Ïß³ÌÆô¶¯Ö®Ç°---¡·false
Ïß³ÌÆô¶¯Ö®ºó---¡·true
Thread-0
Thread-0
Thread-0
Ö÷Ïß³ÌÒ²ÓпÉÄÜÔÚ×ÓÏ߳̽áÊøÖ®Ç°½áÊø¡£²¢ÇÒ×ÓÏ̲߳»ÊÜÓ°Ï죬²»»áÒòΪÖ÷Ï̵߳ĽáÊø¶ø½áÊø¡£
Ï̵߳ÄÇ¿ÖÆÖ´ÐУº
/**
* @author Rollen-Holt Ï̵߳ÄÇ¿ÖÆÖ´ÐÐ
* */
class hello implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String[] args) {
hello he = new hello();
Thread demo = new Thread(he,"Ïß³Ì");
demo.start();
for(int i=0;i<50;++i){
if(i>10){
try{
demo.join(); //Ç¿ÖÆÖ´ÐÐdemo
}catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("main Ïß³ÌÖ´ÐÐ-->"+i);
}
}
} |
¡¾ÔËÐеĽá¹û¡¿£º
main Ïß³ÌÖ´ÐÐ-->0
main Ïß³ÌÖ´ÐÐ-->1
main Ïß³ÌÖ´ÐÐ-->2
main Ïß³ÌÖ´ÐÐ-->3
main Ïß³ÌÖ´ÐÐ-->4
main Ïß³ÌÖ´ÐÐ-->5
main Ïß³ÌÖ´ÐÐ-->6
main Ïß³ÌÖ´ÐÐ-->7
main Ïß³ÌÖ´ÐÐ-->8
main Ïß³ÌÖ´ÐÐ-->9
main Ïß³ÌÖ´ÐÐ-->10
Ïß³Ì
Ïß³Ì
Ïß³Ì
main Ïß³ÌÖ´ÐÐ-->11
main Ïß³ÌÖ´ÐÐ-->12
main Ïß³ÌÖ´ÐÐ-->13
£®£®£®
Ï̵߳ÄÐÝÃߣº
/**
* @author Rollen-Holt Ï̵߳ÄÐÝÃß
* */
class hello implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ i);
}
}
public static void main(String[] args) {
hello he = new hello();
Thread demo = new Thread(he, "Ïß³Ì");
demo.start();
}
} |
¡¾ÔËÐнá¹û¡¿£º£¨½á¹ûÿ¸ô2sÊä³öÒ»¸ö£©
Ïß³Ì0
Ïß³Ì1
Ïß³Ì2
Ï̵߳ÄÖжϣº
/**
* @author Rollen-Holt Ï̵߳ÄÖжÏ
* */
class hello implements Runnable {
public void run() {
System.out.println("Ö´ÐÐrun·½·¨");
try {
Thread.sleep(10000);
System.out.println("Ïß³ÌÍê³ÉÐÝÃß");
} catch (Exception e) {
System.out.println("ÐÝÃß±»´ò¶Ï");
return; //·µ»Øµ½³ÌÐòµÄµ÷Óô¦
}
System.out.println("Ïß³ÌÕý³£ÖÕÖ¹");
}
public static void main(String[] args) {
hello he = new hello();
Thread demo = new Thread(he, "Ïß³Ì");
demo.start();
try{
Thread.sleep(2000);
}catch (Exception e) {
e.printStackTrace();
}
demo.interrupt(); //2sºóÖжÏÏß³Ì
}
} |
¡¾ÔËÐнá¹û¡¿£º
Ö´ÐÐrun·½·¨
ÐÝÃß±»´ò¶Ï
ÔÚjava³ÌÐòÖУ¬Ö»ÒªÇ°Ì¨ÓÐÒ»¸öÏß³ÌÔÚÔËÐУ¬Õû¸öjava³ÌÐò½ø³Ì²»»áСʱ£¬ËùÒÔ´Ëʱ¿ÉÒÔÉèÖÃÒ»¸öºǫ́Ị̈߳¬ÕâÑù¼´Ê¹java½ø³ÌСʱÁË£¬´Ëºǫ́Ïß³ÌÒÀÈ»Äܹ»¼ÌÐøÔËÐС£
/**
* @author Rollen-Holt ºǫ́Ïß³Ì
* */
class hello implements Runnable {
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName()
+ "ÔÚÔËÐÐ");
}
}
public static void main(String[] args) {
hello he = new hello();
Thread demo = new Thread(he, "Ïß³Ì");
demo.setDaemon(true);
demo.start();
}
} |
ËäÈ»ÓÐÒ»¸öËÀÑ»·£¬µ«ÊdzÌÐò»¹ÊÇ¿ÉÒÔÖ´ÐÐÍêµÄ¡£ÒòΪÔÚËÀÑ»·ÖеÄÏ̲߳Ù×÷ÒѾÉèÖÃΪºǫ́ÔËÐÐÁË¡£
Ï̵߳ÄÓÅÏȼ¶£º
/**
* @author Rollen-Holt Ï̵߳ÄÓÅÏȼ¶
* */
class hello implements Runnable {
public void run() {
for(int i=0;i<5;++i){
System.out.println(Thread.currentThread().getName()+"ÔËÐÐ"+i);
}
}
public static void main(String[] args) {
Thread h1=new Thread(new hello(),"A");
Thread h2=new Thread(new hello(),"B");
Thread h3=new Thread(new hello(),"C");
h1.setPriority(8);
h2.setPriority(2);
h3.setPriority(6);
h1.start();
h2.start();
h3.start();
}
} |
¡¾ÔËÐнá¹û¡¿£º
AÔËÐÐ0
AÔËÐÐ1
AÔËÐÐ2
AÔËÐÐ3
AÔËÐÐ4
BÔËÐÐ0
CÔËÐÐ0
CÔËÐÐ1
CÔËÐÐ2
CÔËÐÐ3
CÔËÐÐ4
BÔËÐÐ1
BÔËÐÐ2
BÔËÐÐ3
BÔËÐÐ4
µ«ÊÇÇë¶ÁÕß²»ÒªÎóÒÔΪÓÅÏȼ¶Ô½¸ß¾ÍÏÈÖ´ÐС£ËÏÈÖ´Ðл¹ÊÇÈ¡¾öÓÚËÏÈÈ¥µÄCPUµÄ×ÊÔ´¡¢ÁíÍ⣬Ö÷Ï̵߳ÄÓÅÏȼ¶ÊÇ5.Ï̵߳ÄÀñÈá£
ÔÚÏ̲߳Ù×÷ÖУ¬Ò²¿ÉÒÔʹÓÃyield£¨£©·½·¨£¬½«Ò»¸öÏ̵߳IJÙ×÷ÔÝʱ½»¸øÆäËûÏß³ÌÖ´ÐС£
/**
* @author Rollen-Holt Ï̵߳ÄÓÅÏȼ¶
* */
class hello implements Runnable {
public void run() {
for(int i=0;i<5;++i){
System.out.println(Thread.currentThread().getName()+"ÔËÐÐ"+i);
if(i==3){
System.out.println("Ï̵߳ÄÀñÈÃ");
Thread.currentThread().yield();
}
}
}
public static void main(String[] args) {
Thread h1=new Thread(new hello(),"A");
Thread h2=new Thread(new hello(),"B");
h1.start();
h2.start();
}
} |
AÔËÐÐ0
AÔËÐÐ1
AÔËÐÐ2
AÔËÐÐ3
Ï̵߳ÄÀñÈÃ
AÔËÐÐ4
BÔËÐÐ0
BÔËÐÐ1
BÔËÐÐ2
BÔËÐÐ3
Ï̵߳ÄÀñÈÃ
BÔËÐÐ4
ͬ²½ºÍËÀËø£º
¡¾ÎÊÌâÒý³ö¡¿:±ÈÈç˵¶ÔÓÚÂòƱϵͳ£¬ÓÐÏÂÃæµÄ´úÂ룺
/**
* @author Rollen-Holt
* */
class hello implements Runnable {
public void run() {
for(int i=0;i<10;++i){
if(count>0){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(count--);
}
}
}
public static void main(String[] args) {
hello he=new hello();
Thread h1=new Thread(he);
Thread h2=new Thread(he);
Thread h3=new Thread(he);
h1.start();
h2.start();
h3.start();
}
private int count=5;
} |
¡¾ÔËÐнá¹û¡¿£º
5
4
3
2
1
0
-1
ÕâÀï³öÏÖÁË-1£¬ÏÔÈ»Õâ¸öÊÇ´íµÄ¡££¬Ó¦¸ÃƱÊý²»ÄÜΪ¸ºÖµ¡£
Èç¹ûÏë½â¾öÕâÖÖÎÊÌ⣬¾ÍÐèҪʹÓÃͬ²½¡£Ëùνͬ²½¾ÍÊÇÔÚͳһʱ¼ä¶ÎÖÐÖ»ÓÐÓÐÒ»¸öÏß³ÌÔËÐУ¬
ÆäËûµÄÏ̱߳ØÐëµÈµ½Õâ¸öÏ߳̽áÊøÖ®ºó²ÅÄܼÌÐøÖ´ÐС£
¡¾Ê¹ÓÃÏß³Ìͬ²½½â¾öÎÊÌâ¡¿
²ÉÓÃͬ²½µÄ»°£¬¿ÉÒÔʹÓÃͬ²½´úÂë¿éºÍͬ²½·½·¨Á½ÖÖÀ´Íê³É¡£
¡¾Í¬²½´úÂë¿é¡¿£º
Óï·¨¸ñʽ£º
synchronized£¨Í¬²½¶ÔÏó£©{
//ÐèҪͬ²½µÄ´úÂë
}
|
µ«ÊÇÒ»°ã¶¼°Ñµ±Ç°¶ÔÏóthis×÷Ϊͬ²½¶ÔÏó¡£
±ÈÈç¶ÔÓÚÉÏÃæµÄÂòƱµÄÎÊÌ⣬ÈçÏ£º
/**
* @author Rollen-Holt
* */
class hello implements Runnable {
public void run() {
for(int i=0;i<10;++i){
synchronized (this) {
if(count>0){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(count--);
}
}
}
}
public static void main(String[] args) {
hello he=new hello();
Thread h1=new Thread(he);
Thread h2=new Thread(he);
Thread h3=new Thread(he);
h1.start();
h2.start();
h3.start();
}
private int count=5;
} |
¡¾ÔËÐнá¹û¡¿£º£¨Ã¿Ò»ÃëÊä³öÒ»¸ö½á¹û£©
5
4
3
2
1
¡¾Í¬²½·½·¨¡¿
Ò²¿ÉÒÔ²ÉÓÃͬ²½·½·¨¡£
Óï·¨¸ñʽΪsynchronized ·½·¨·µ»ØÀàÐÍ·½·¨Ãû£¨²ÎÊýÁÐ±í£©{
// ÆäËû´úÂë
}
ÏÖÔÚ£¬ÎÒÃDzÉÓÃͬ²½·½·¨½â¾öÉÏÃæµÄÎÊÌâ¡£
/**
* @author Rollen-Holt
* */
class hello implements Runnable {
public void run() {
for (int i = 0; i < 10; ++i) {
sale();
}
}
public synchronized void sale() {
if (count > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(count--);
}
}
public static void main(String[] args) {
hello he = new hello();
Thread h1 = new Thread(he);
Thread h2 = new Thread(he);
Thread h3 = new Thread(he);
h1.start();
h2.start();
h3.start();
}
private int count = 5;
} |
¡¾ÔËÐнá¹û¡¿£¨Ã¿ÃëÊä³öÒ»¸ö£©
5
4
3
2
1
ÌáÐÑһϣ¬µ±¶à¸öÏ̹߳²ÏíÒ»¸ö×ÊÔ´µÄʱºòÐèÒª½øÐÐͬ²½£¬µ«Êǹý¶àµÄͬ²½¿ÉÄܵ¼ÖÂËÀËø¡£
´Ë´¦ÁоپµäµÄÉú²úÕߺÍÏû·ÑÕßÎÊÌâ¡£
¡¾Éú²úÕߺÍÏû·ÑÕßÎÊÌâ¡¿
ÏÈ¿´Ò»¶ÎÓÐÎÊÌâµÄ´úÂë¡£
class Info {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name = "Rollen";
private int age = 20;
}
/**
* Éú²úÕß
* */
class Producer implements Runnable{
private Info info=null;
Producer(Info info){
this.info=info;
}
public void run(){
boolean flag=false;
for(int i=0;i<25;++i){
if(flag){
this.info.setName("Rollen");
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
this.info.setAge(20);
flag=false;
}else{
this.info.setName("chunGe");
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
this.info.setAge(100);
flag=true;
}
}
}
}
/**
* Ïû·ÑÕßÀà
* */
class Consumer implements Runnable{
private Info info=null;
public Consumer(Info info){
this.info=info;
}
public void run(){
for(int i=0;i<25;++i){
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.info.getName()+"<---->"+this.info.getAge());
}
}
}
/**
* ²âÊÔÀà
* */
class hello{
public static void main(String[] args) {
Info info=new Info();
Producer pro=new Producer(info);
Consumer con=new Consumer(info);
new Thread(pro).start();
new Thread(con).start();
}
} |
¡¾ÔËÐнá¹û¡¿£º
Rollen<---->100
chunGe<---->20
chunGe<---->100
Rollen<---->100
chunGe<---->20
Rollen<---->100
Rollen<---->100
Rollen<---->100
chunGe<---->20
chunGe<---->20
chunGe<---->20
Rollen<---->100
chunGe<---->20
Rollen<---->100
chunGe<---->20
Rollen<---->100
chunGe<---->20
Rollen<---->100
chunGe<---->20
Rollen<---->100
chunGe<---->20
Rollen<---->100
chunGe<---->20
Rollen<---->100
chunGe<---->20
´ó¼Ò¿ÉÒÔ´Ó½á¹ûÖп´µ½£¬Ãû×ÖºÍÄêÁ䲢ûÓжÔÓÚ¡£
ÄÇôÈçºÎ½â¾öÄØ£¿
1£©¼ÓÈëͬ²½
2£©¼ÓÈëµÈ´ýºÍ»½ÐÑ
ÏÈÀ´¿´¿´¼ÓÈëͬ²½»áÊÇÈçºÎ¡£
class Info {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public synchronized void set(String name, int
age){
this.name=name;
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
this.age=age;
}
public synchronized void get(){
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.getName()+"<===>"+this.getAge());
}
private String name = "Rollen";
private int age = 20;
}
/**
* Éú²úÕß
* */
class Producer implements Runnable {
private Info info = null;
Producer(Info info) {
this.info = info;
}
public void run() {
boolean flag = false;
for (int i = 0; i < 25; ++i) {
if (flag) {
this.info.set("Rollen", 20);
flag = false;
} else {
this.info.set("ChunGe", 100);
flag = true;
}
}
}
}
/**
* Ïû·ÑÕßÀà
* */
class Consumer implements Runnable {
private Info info = null;
public Consumer(Info info) {
this.info = info;
}
public void run() {
for (int i = 0; i < 25; ++i) {
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.info.get();
}
}
}
/**
* ²âÊÔÀà
* */
class hello {
public static void main(String[] args) {
Info info = new Info();
Producer pro = new Producer(info);
Consumer con = new Consumer(info);
new Thread(pro).start();
new Thread(con).start();
}
} |
¡¾ÔËÐнá¹û¡¿£º
Rollen<===>20
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
ChunGe<===>100
´ÓÔËÐнá¹ûÀ´¿´£¬´íÂÒµÄÎÊÌâ½â¾öÁË£¬ÏÖÔÚÊÇRollen ¶ÔÓ¦20£¬ChunGe¶ÔÓÚ100£¬µ«ÊÇ»¹ÊdzöÏÖÁËÖØ¸´¶ÁÈ¡µÄÎÊÌ⣬Ҳ¿Ï¶¨ÓÐÖØ¸´¸²¸ÇµÄÎÊÌâ¡£Èç¹ûÏë½â¾öÕâ¸öÎÊÌ⣬¾ÍÐèҪʹÓÃObjectÀà°ïæÁË£¬ÎÒÃÇ¿ÉÒÔʹÓÃÆäÖеĵȴýºÍ»½ÐѲÙ×÷¡£
ÒªÍê³ÉÉÏÃæµÄ¹¦ÄÜ£¬ÎÒÃÇÖ»ÐèÒªÐÞ¸ÄInfoÀ༢¿Ê£¬ÔÚÆäÖмÓÉϱê־룬²¢ÇÒͨ¹ýÅжϱê־λÍê³ÉµÈ´ýºÍ»½ÐѵIJÙ×÷£¬´úÂëÈçÏ£º
class Info {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public synchronized void set(String name, int
age){
if(!flag){
try{
super.wait();
}catch (Exception e) {
e.printStackTrace();
}
}
this.name=name;
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
this.age=age;
flag=false;
super.notify();
}
public synchronized void get(){
if(flag){
try{
super.wait();
}catch (Exception e) {
e.printStackTrace();
}
}
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.getName()+"<===>"+this.getAge());
flag=true;
super.notify();
}
private String name = "Rollen";
private int age = 20;
private boolean flag=false;
}
/**
* Éú²úÕß
* */
class Producer implements Runnable {
private Info info = null;
Producer(Info info) {
this.info = info;
}
public void run() {
boolean flag = false;
for (int i = 0; i < 25; ++i) {
if (flag) {
this.info.set("Rollen", 20);
flag = false;
} else {
this.info.set("ChunGe", 100);
flag = true;
}
}
}
}
/**
* Ïû·ÑÕßÀà
* */
class Consumer implements Runnable {
private Info info = null;
public Consumer(Info info) {
this.info = info;
}
public void run() {
for (int i = 0; i < 25; ++i) {
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.info.get();
}
}
}
/**
* ²âÊÔÀà
* */
class hello {
public static void main(String[] args) {
Info info = new Info();
Producer pro = new Producer(info);
Consumer con = new Consumer(info);
new Thread(pro).start();
new Thread(con).start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
¡¾³ÌÐòÔËÐнá¹û¡¿£º
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ChunGe<===>100
Rollen<===>20
ÏÈÔÚ¿´½á¹û¾Í¿ÉÒÔÖªµÀ£¬Ö®Ç°µÄÎÊÌâÍêÈ«½â¾ö¡£
|
±¾ÈËÉî֪ѧµÄÌ«²î£¬ËùÒÔÏ£Íû´ó¼ÒÄܶà¶àÖ¸µã¡£ÁíÍ⣬¹ØÓÚ¶àÏß³ÌÆäʵÓкܶàµÄ֪ʶ£¬ÓÉÓÚĿǰÎÒÒ²¾ÍÖªµÀµÄ²»Ì«¶à£¬Ð´ÁËһЩ³£Óõġ£ËäÈ»ÔÚ²Ù×÷ϵͳÕâÃÅ¿ÎÉÏѧÁ˺ܶàµÄÏ̺߳ͽø³Ì£¬±ÈÈçÒøÐмÒËã·¨µÈµÈµÄ£¬ÒÔºóÓÐʱ¼äÔÚ²¹³ä£¬´ó¼ÒÓÐʲôºÃ×ÊÁÏ¿ÉÒÔÁô¸öÑÔ£¬´ó¼ÒÒ»Æð·ÖÏíһϣ¬Ð»Ð»ÁË¡£
|