Thread Summary 1

Source: Internet
Author: User

two Java Declaration implementation methods of thread 1
A inherits thread
B implements runnable
example
public class testthread1 {
Public static void main (string ARGs []) {
runner1 r = new runner1 ();
r. start ();
// thread t = new thread (r);
// T. start ();
for (INT I = 0; I <100; I ++) {
system. out. println ("main thread: ------" + I);
}< BR >}

// class runner1 implements runnable {
class runner1 extends thread {
Public void run () {
for (INT I = 0; I <100; I ++) {
system. out. println ("runner1:" + I);
}< BR >}< br>
2 sleep usage
Import Java. util. *;
public class testsleep {
Public static void main (string [] ARGs) {
mythread thread = new mythread ();
thread. start ();
try {thread. sleep (10000) ;}< br> catch (interruptedexception e) {}< br> thread. interrupt ();
}< BR >}

Class mythread extends thread {
Boolean flag = true;
Public void run (){
While (FLAG ){
System. Out. println ("=" + new date () + "= ");
Try {
Sleep (1000 );
} Catch (interruptedexception e ){
Return;
}
}
}
}

3 join: After the sub-thread is executed, the main thread will execute
Public class testjoin {
Public static void main (string [] ARGs ){
Mythread2 T1 = new mythread2 ("ABCDE ");
T1.start ();
Try {
T1.join ();
} Catch (interruptedexception e ){}

For (INT I = 1; I <= 10; I ++ ){
System. Out. println ("I am main thread ");
}
}
}
Class mythread2 extends thread {
Mythread2 (string s ){
Super (s );
}

Public void run (){
For (INT I = 1; I <= 10; I ++ ){
System. Out. println ("I am" + getname ());
Try {
Sleep (1000 );
} Catch (interruptedexception e ){
Return;
}
}
}
}

4 yield (): Give the output thread to another
public class testyield {
Public static void main (string [] ARGs) {
mythread3 T1 = new mythread3 ("T1");
mythread3 t2 = new mythread3 ("T2");
t1.start (); t2.start ();
}< BR >}< br> class mythread3 extends thread {
mythread3 (string s) {super (s) ;}< br> Public void run () {
for (INT I = 1; I <= 100; I ++) {
system. out. println (getname () + ":" + I);
if (I % 10 = 0) {
yield ();
}< BR >}

5. thread priority

Public class testpriority {
Public static void main (string [] ARGs ){
Thread T1 = new thread (New T1 ());
Thread t2 = new thread (New T2 ());
T1.setpriority (thread. norm_priority + 3 );
T1.start ();
T2.start ();
}
}

Class T1 implements runnable {
Public void run (){
For (INT I = 0; I <1000; I ++ ){
System. Out. println ("T1:" + I );
}
}
}

Class T2 implements runnable {
Public void run (){
For (INT I = 0; I <1000; I ++ ){
System. Out. println ("------ t2:" + I );
}
}
}

6-thread synchronized.
public class testsync implements runnable {
timer = new timer ();
Public static void main (string [] ARGs) {
testsync test = new testsync ();
thread T1 = new thread (TEST);
thread t2 = new thread (test );
t1.setname ("T1");
t2.setname ("T2");
t1.start ();
t2.start ();
}< br> Public void run () {
timer. add (thread. currentthread (). getname ();
}< BR >}

Class timer {
Private Static int num = 0;
Public synchronized void add (string name ){
// Synchronized (this ){
Num ++;
Try {thread. Sleep (1 );}
Catch (interruptedexception e ){}
System. Out. println (name + ", you are the" + num + "thread using timer ");
//}
}
}

Note: because both T1 and T2 threads use timer in the public resource area at the same time, synchronization will occur. You must add synchronized.

7. deadlock example
Public class testdeadlock implements runnable {
Public int flag = 1;
Static object O1 = new object (), O2 = new object ();
Public void run (){
System. Out. println ("flag =" + flag );
If (flag = 1 ){
Synchronized (O1 ){
Try {
Thread. Sleep (500 );
} Catch (exception e ){
E. printstacktrace ();
}
Synchronized (O2 ){
System. Out. println ("1 ");
}
}
}
If (flag = 0 ){
Synchronized (O2 ){
Try {
Thread. Sleep (500 );
} Catch (exception e ){
E. printstacktrace ();
}
Synchronized (O1 ){
System. Out. println ("0 ");
}
}
}
}

Public static void main (string [] ARGs ){
Testdeadlock TD1 = new testdeadlock ();
Testdeadlock td2 = new testdeadlock ();
Td1.flag = 1;
Td2.flag = 0;
Thread T1 = new thread (TD1 );
Thread t2 = new thread (td2 );
T1.start ();
T2.start ();

}
}

8. For example Program
Public class sync implements runnable {
Int B = 100;

Public synchronized void M1 () throws exception {
// Thread. Sleep (2000 );
B = 1000;
Thread. Sleep (5000 );
System. Out. println ("B =" + B );
}

Public synchronized void m2 () throws exception {
Thread. Sleep (2500 );
B = 2000;
}

Public void run (){
Try {
M1 ();
} Catch (exception e ){
E. printstacktrace ();
}
}

Public static void main (string [] ARGs) throws exception {
Sync TT = new sync ();
Thread t = new thread (TT );
T. Start ();

TT. m2 ();
System. Out. println (TT. B );
}
}
Output: 1000
B = 1000
Because after m2 is started, the lock is obtained, and then the M1 is executed after the lock is released, and finally B = 1000.
If public void m2 () throws exception {
Then B = 2000, always at the end of B = 2000


9 producer and consumer problems
Public class producerconsumer {
Public static void main (string [] ARGs ){
Syncstack Ss = new syncstack ();
Producer P = new producer (SS );
Consumer c = new consumer (SS );
New thread (P). Start ();
New thread (P). Start ();
New thread (P). Start ();
New thread (c). Start ();
}
}

Class wotou {
Int ID;
Wotou (int id ){
This. ID = ID;
}
Public String tostring (){
Return "wotou:" + ID;
}
}

class syncstack {
int Index = 0;
wotou [] arrwt = new wotou [6];
Public synchronized void push (wotou wt) {
while (Index = arrwt. length) {
try {
This. wait ();
}catch (interruptedexception e) {
E. printstacktrace ();
}< BR >}< br> This. policyall ();
arrwt [Index] = wt;
index ++;
}< br>
Public synchronized wotou POP () {
while (Index = 0) {
try {
This. wait ();
}catch (interruptedexception e) {
E. printstacktrace ();
}< BR >}< br> This. policyall ();
index --;
return arrwt [Index];
}< BR >}

class producer implements runnable {
syncstack Ss = NULL;
producer (syncstack SS) {
This. ss = SS;
}< br>
Public void run () {
for (INT I = 0; I <20; I ++) {
wotou Wt = new wotou (I);
SS. push (wt);
system. out. println ("produced:" + wt);
try {
thread. sleep (INT) (math. random () * 200);
}catch (interruptedexception e) {
E. printstacktrace ();
}< BR >}

class Consumer implements runnable {
syncstack Ss = NULL;
consumer (syncstack SS) {
This. ss = SS;
}< br>
Public void run () {
for (INT I = 0; I <20; I ++) {
wotou Wt = ss. pop ();
system. out. println ("consumed:" + wt);
try {
thread. sleep (INT) (math. random () * 1000);
}catch (interruptedexception e) {
E. printstacktrace ();
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.