Examples of Java threading operations

Source: Internet
Author: User

One: Example of threading operations ( 1 )

Package com.cloud.day6;

public class Demo1 {

publicstatic void Main (string[] args) {

Mythreadmt1=new MyThread ("Thread A", 1000);

Mythreadmt2=new MyThread ("Thread B", 2000);

Mythreadmt3=new MyThread ("Thread C", 3000);

Mt1.start ();

Mt2.start ();

Mt3.start ();

}

}

Class MyThread extends thread{

Privateint time;

Publicmythread (String name,int time) {

Super (name);

This.time=time;

}

Publicvoid Run () {

try{

Thread.Sleep (This.time);

}catch (Exception e) {

E.printstacktrace ();

}

System.out.println (Thread.CurrentThread (). GetName () + ", hibernate" +this.time);

}

}

Two: Example of threading operations ( 2 )

Package com.cloud.day6;

public class Demo1 {

publicstatic void Main (string[] args) {

Mythreadmt1=new MyThread ("Thread A", 1000);

Mythreadmt2=new MyThread ("Thread B", 2000);

Mythreadmt3=new MyThread ("Thread C", 3000);

Newthread (MT1). Start ();

Newthread (MT2). Start ();

Newthread (MT3). Start ();

}

}

Class MyThread implements runnable{

Privatestring name;

Privateint time;

Publicmythread (String name,int time) {

This.name=name;

This.time=time;

}

Publicvoid Run () {

try{

Thread.Sleep (This.time);

}catch (Exception e) {

E.printstacktrace ();

}

System.out.println (this.name+ ", dormant" +this.time);

}

}

Three: Sync ticket problem in thread

Package com.cloud.day6;

public class Demo1 {

publicstatic void Main (string[] args) {

Mythreadmt1=new MyThread ();

Threadt1=new Thread (MT1);

Threadt2=new Thread (MT1);

Threadt3=new Thread (MT1);

T1.start ();

T2.start ();

T3.start ();

}

}

Class MyThread implements runnable{

Privateint ticket=5;

Publicvoid Run () {

for (inti=0;i<100;i++) {

This.sale ();

}

}

publicsynchronized void Sale () {

if (ticket>0) {

try{

Thread.Sleep (3000);

}catch (Interruptedexception e) {

E.printstacktrace ();

}

System.out.println ("sold a ticket, left the ticket:" + (--ticket) + "Zhang");

}

}

}

Four: Thread deadlock problem

That is, two threads are waiting for each other to execute

Packagecom.cloud.day6;

Publicclass Demo2 {

public static void Main (string[] args) {

MyThread1 mt1=new MyThread1 ();

MyThread1 mt2=new MyThread1 ();

Mt1.flag=true;

Mt2.flag=false;

Thread th1=new thread (MT1);

Thread th2=new thread (MT2);

Th1.start ();

Th2.start ();

}

}

classzhansan{

public void Say () {

System.out.println ("Zhang San: you draw me, I give you the book");

}

public void get () {

System.out.println ("Zhang San Get Painted");

}

}

classlisi{

public void Say () {

SYSTEM.OUT.PRINTLN ("John Doe: You give me books, I draw for You");

}

public void get () {

SYSTEM.OUT.PRINTLN ("John Doe got the book");

}

}

CLASSMYTHREAD1 Implements runnable{

private static Zhansan zs=new Zhansan ();

private static Lisi is=new Lisi ();

public Boolean flag=false;

@Override

public void Run () {

if (flag) {

Synchronized (ZS) {

Zs.say ();

try {

Thread.Sleep (500);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

Synchronized (IS) {

Zs.get ();

}

}

}

else{

Synchronized (IS) {

Is.say ();

try {

Thread.Sleep (500);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

Synchronized (ZS) {

Is.get ();

}

}

}

}

}

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

Examples of Java threading operations

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.