Lab 10: thread (6 hours)

Source: Internet
Author: User

Lab Purpose

(1) understand the concept and basic operation methods of Multithreading

(2) master the thread Concurrency Control Technology

(3) Master Thread Synchronization Technology

Lab content and requirements

1. Write a program and create five threads, each of which displays five different strings. They are implemented by inheriting the Thread class and implementing the Runnable interface.

 

 

 

View Code

 1    /**
2 *
3 * @author FREESKYC
4 */
5 public class NewClass {
6 public static void main(String args[]) throws Exception {
7 int i = 0;
8 Hello t = new Hello();
9 Hello t1 = new Hello();
10 Hello t2 = new Hello();
11 Hello t3 = new Hello();
12 Hello t4 = new Hello();
13 t1.start();
14 t2.start();
15 t3.start();
16 t4.start();
17 t.start();
18 }
19 }
20 class Hello extends Thread {
21 int i;
22 public void run() {
23 while (true) {
24 System.out.println("Hello" + i++);
25 if (i == 5) {
26 break;
27 }
28 }
29 }
30 }
31 /**
32 *
33 * @author FREESKYC
34 */
35 public class NewClass {
36 public static void main(String args[]) throws Exception {
37 int i = 0;
38 Hello h1 = new Hello("idgjadkgk");
39 Thread t1 = new Thread(h1);
40 t1.start();
41 Hello h2 = new Hello("dpgkaldl");
42 Thread t2 = new Thread(h2);
43 t2.start();
44 Hello h3 = new Hello("oglrlel");
45 Thread t3 = new Thread(h3);
46 t3.start();
47 Hello h4 = new Hello("iddk");
48 Thread t4 = new Thread(h4);
49 t4.start();
50 Hello h = new Hello("ddddd");
51 Thread t = new Thread(h);
52 t.start();
53 }
54 }
55 class Hello implements Runnable {
56 String i = "";
57 Hello(String str) {
58 this.i = str;
59 }
60 public void run() {
61 System.out.println(i);
62 }
63 }

 

 

 

 

2. Compile a producer/consumer program. The producer generates a value of 0 ~ Every Ms ~ A number between 9 is saved in a MyNumber type object and displayed. As long as the new number is saved in the MyNumber object, the consumer will display it. Define the MyNumber class, write the consumer and producer programs, and write the main program to create a MyNumber object, a producer thread, a consumer thread, and start these two threads.

 

   /**
*
* @author FREESKYC
*/
public class NewClass {

public static void main(String[] args) {
MyNumber myber = new MyNumber();
produ t1 = new produ(myber);
consu t2 = new consu(myber);
Thread th1 = new Thread(t1);
Thread th2 = new Thread(t2);
th1.start();
th2.start();
}
}

class produ implements Runnable {

MyNumber num;

produ(MyNumber ber) {
this.num = ber;
}

public void run() {
for (int i = 0; i <= 10; i++) {
synchronized (num) {
if (num.str) {
int x = (int) (Math.random() * 9);
System.out.print("produ:");
num.setnumber(x);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
num.str = false;
num.notify();
} else {
try {
num.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
if (i == 10) {
System.exit(0);
}
}
}

}
}

class consu implements Runnable {

MyNumber num;

consu(MyNumber myber) {
this.num = myber;
}

@Override
public void run() {
for (int i = 0; i <= 10; i++) {
synchronized (num) {
if (!num.str) {
System.out.print("consu:");
System.out.println(num.getnumber());
num.str = true;
num.notify();
} else {
try {
num.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (i == 10) {
System.exit(0);
}
}
}
}

class MyNumber {

int number;
boolean str = true;

public void setnumber(int number) {
this.number = number;
System.out.println(number);
}

public int getnumber() {
return number;
}
}

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.