// Thread synchronization exercise public class threadnosynchronized {public static void main (string argv []) {synchronized data oshare = new synchronized data (); // create, initialize the metadata Data Object oshare threaddemo Th1 = new threaddemo ("thread1", oshare); // create the thread Th1 threaddemo Th1 = new threaddemo ("thread2", oshare ); // create thread 2th1.start (); // start thread Th1 th2.start (); // start thread 2's} class 1_data {public static string szdata = ""; // declare, and initialize the string data field as the shared data} class threaddemo extends thread {private external data oshare; // declaration, and initialize the threaddemo () {}// Declaration of the external data field, threaddemo constructor // declaration is implemented, and threaddemo constructor with parameters threaddemo (string szname, invalid data oshare) {super (szname ); // call the constructor of the parent class this. oshare = oshare; // initialize the oshare domain} @ suppresswarnings ("static-access") Public void run () {// synchronized (oshare) {// Add this line of code, implement thread synchronization and the threads are executed in an orderly manner. // when the code is not added to the thread asynchronously, a "thread race" phenomenon occurs. For (INT I = 0; I <5; I ++) {If (this. getname (). equals ("thread1") {oshare. szdata = "this is 1st Threads"; // to demonstrate the problem, set a sleep try {thread. sleep (INT) math. random () * 100); // sleep} catch (interruptedexception e) {// catch exception E. printstacktrace ();} system. out. println (this. getname () + ":" + oshare. szdata); // output string information} else if (this. getname (). equals ("thread2") {oshare. szdata = "this is 2nd Threads"; // to demonstrate the problem, set a sleep try {thread. sleep (INT) math. random () * 100); // thread sleep} catch (interruptedexception e) {// catch exception E. printstacktrace ();} system. out. println (this. getname () + ":" + oshare. szdata); // output string information }}//}}}
// Thread priority exercise public class threadpriority {public static void main (string ARGs []) {// use the subclass of the thread class to create the thread inheritthread ITD = new inheritthread (); // use the runnable interface class object to create the thread RTD = new thread (New runnablethread (); ITD. setpriority (5); // set the priority of mythread1 to 5 RTD. setpriority (5); // set the priority of mythread2 to 5 ITD. start (); // start the itd rtd thread. start (); // start thread RTD }}// set the thread priority class inheritthread extends thread {// The run () method of the custom thread public void run () {system. out. println ("I am inheritthread"); // output string information for (INT I = 0; I <10; I ++) {system. out. println ("inheritthread: I =" + I); // output information try {thread. sleep (INT) math. random () * 1000); // thread sleep, random number within 1000} catch (interruptedexception E) // catch exceptions {}}}// another thread class runnablethread implements runnable created through the runnable interface // The run () method of the custom thread public void run () {system. out. println ("I am runnablethread"); // output string information for (INT I = 0; I <10; I ++) {system. out. println ("runnablethread: I =" + I); // output I try {thread. sleep (INT) math. random () * 1000); // thread sleep. The sleep time is a random number within 1000} catch (interruptedexception e) {// catch an exception }}}}
/* Multi-thread communication this example simulates the relationship between producers and consumers. The consumer is in the waiting state when calling the consumption method, which calls the producer thread. After the producer starts to produce shared data, the consumer consumes the data, but when the shared data is empty, all consumers must wait, the producer continues to produce the data, and then the consumer consumes the data again, so that the program runs until the end of the process, we can see that the thread has been waiting. Note that this thread does not wake up after it enters the waiting state. It waits until it is forced out of the JVM environment. Note: considering program security, notifiall () is used in most cases unless it is clear which thread to wake up. The precondition for wait method calling is that the current thread obtains the Lock of this object, that is, the wait method must be placed in the synchronization block or synchronization method. */Public class threadcommunication {public static void main (string [] ARGs) {queue q = new Queue (); // create, and initialize a queue producer P = new producer (Q); // create and initialize a producer consumer c = new consumer (Q); // create and initialize a consumer C. start (); // start the consumer thread p. start (); // producer thread start} // producer consumer Thread class producer extends thread {// implement producer thread queue Q; // declare queue qproducer (queue q) {// producer constructor method this. Q = Q; // queue Q initialization} public void run () {for (INT I = 1; I <5; I ++) {// Add element Q cyclically. put (I); // Add new elements to the queue} class Consumer extends thread {queue Q; // declare the queue qconsumer (queue q) {// consumer constructor this. Q = Q; // queue Q initialization} public void run () {While (true) {// cyclic consumption element Q. get (); // get the elements in the queue }}} class queue {int value = 0; // declare and initialize the integer data field value Boolean isempty = true; // declare, initialize the boolean data domain isempty to determine the queue status // producer production method public synchronized void put (INT v) {// if the shared data is not consumed, then the producer waits for if (! Isempty) {try {system. out. println ("producer wait"); wait (); // enters the wait state} catch (exception e) {// catch exception E. printstacktrace (); // exception information output} value + = V; // value plus V isempty = false; // isempty is assigned to false system. out. println ("Total number of producers:" + V); // output string information notify (); // notify the consumer to consume after production} // method of consumption by the consumer public synchronized int get () {// if the shared data has been consumed before consumption by the consumer, then the consumer waits for if (isempty) {try {system. out. println ("Consumer wait"); // output string information wait (); // enters the wait state} catch (exception e) {// catch exception E. printstacktrace (); // exception information output} value --; // value-1 If (value <1) {isempty = true; // isempty value true} system. out. println ("one consumer consumes, remaining:" + value); // output information y (); // After the consumer consumes, the producer is notified to produce return value; // return value }}