1. Implement Runnable interface
Synchronizing code blocks
Use the Synchronized keyword to synchronize the declaration of a block of code, but when you use this operation you must explicitly indicate which object is to be locked, typically based on the current object.
Synchronized (object) {//Generally speaking this lock
Lock Object
}
Using a synchronized code block to resolve:
Package Org.dennist.thread.demo; /** * * Ticketthreadr.java * * @version: 1.1 * * @author: Sujonin <a href= "mailto:dennisit@163.com" ; Send Mail </a> * * @since: 1.0 Create Time: 2013-2-24 pm 02:29:23 * * Todo:class Ti Cketthreadr.java is used for ... * */public class TICKETTHREADR implements runnable{private int num = 5;
Total number of votes set to 5 @Override public void Run () {for (int i=0; i<10; i++) {//Use synchronized code block Synchronized (this) {try {thread.sleep (300);
Rest 300 milliseconds} catch (Interruptedexception e) {e.printstacktrace (); } if (this.num>0) {//Print ticket information System.out.println (Threa
D.currentthread () getName () + "Buy Tickets:" + this.num--); public static void Main (S)}}}Tring[] args {ticketthreadr ticketthread = new Ticketthreadr (); New Thread (Ticketthread, "Ticket outlet One"). Start (); Threads a new Thread (Ticketthread, "Ticket Gate Two"). Start (); Thread two new thread (Ticketthread, "ticket port Three"). Start (); Thread Three}}
Synchronization Method
The synchronization method is to add synchronized keyword modification to the method
The above question uses the synchronized code block to solve
Package Org.dennist.thread.demo; /** * * Ticketthreadr.java * * @version: 1.1 * * @author: Sujonin <a href= "mailto:dennisit@163.com" ; Send Mail </a> * * @since: 1.0 Create Time: 2013-2-24 pm 02:29:23 * * Todo:class Ti Cketthreadr.java is used for ... * */public class TICKETTHREADR implements runnable{private int num = 5; The total number of votes is set to 5 @Override public void Run () {for (int i=0; i<10; i++) {sale ();
Invoke Sync Method}///Use sync method public synchronized void sale () {try { Thread.Sleep (300);
Rest 300 milliseconds} catch (Interruptedexception e) {e.printstacktrace (); } if (this.num>0) {//Print ticket information System.out.println (Thread.CurrentThread (). Getnam
E () + "Buy Tickets:" + this.num--); } public static void Main (string[] args) {Ticketthreadr tickEtthread = new Ticketthreadr (); New Thread (Ticketthread, "Ticket outlet One"). Start (); Threads a new Thread (Ticketthread, "Ticket Gate Two"). Start (); Threads a new Thread (Ticketthread, "ticket port Three"). Start (); Thread One}}
2. Inherit Thread class
Error Analysis:
Class Thread2 extends Thread {
int n = m;
public void Run () {method
();
}
Public synchronized void Method () {while
(n > 0) {
System.out.println (n--);
}
}} Class Test {public
static void Main (string[] args) {
Thread2 t1 = new Thread2 ();
Thread2 t2 = new Thread2 ();
Thread2 t3 = new Thread2 ();
Thread2 T4 = new Thread2 ();
T1.start ();
T2.start ();
T3.start ();
T4.start ();
}
The following code does not achieve the effect of synchronization, although the method uses the synchronized modifier, but each time it comes out of the new keyword is a different object, their monitor objects are different, yes. So how do you make these four objects (T1, T2, T3, T4) Use the same monitor to achieve the sync effect?
Class Somethread extends Thread {
Object locker;
Public Somethread (Object Locker) {
this.locker = locker;
}
public void Run () {
synchronized (locker) {//locker resources that are jointly accessed can be
//do something here
}
}} public class Test {public
static void Main (string[] args) {
object locker = new Object ();
thread[] t = new thread[10];
for (int i=0; i<t.length; i++) {
T[i] = new Somethread (locker);//Pass the common resource object to the thread
T[i].start ();
}
}
As long as the common access to lock the resource object can be, if you want to lazy, direct synchronized (Somethread.class), lock class objects, but the lock class object sacrifice too much, performance has declined, so easy to lock class objects.