实验题目:多线程
目的与要求:熟悉多线程的创建以及线程的同步。
内容:
编写一个应用程序,除了主线程外,还有两个子线程。两个子线程对同一个数据操作,其中一个线程负责对该数据做递增操作,一个线程负责对该线程做递减操作。当这个数据小于0的话,递减操作等待,当这个数据大于100的话,递增操作等待。
package angry;
class SyncStack { // 同步堆栈类
private int index = 0; // 堆栈指针初始值为0
private int[] buffer = new int[1]; // 堆栈有6个字符的空间
public synchronized void push(int c) { // 加上互斥锁
while (index == buffer.length) { // 堆栈已满,不能压栈
try {
this.wait(); // 等待,直到有数据出栈
} catch (InterruptedException e) {
}
}
this.notify(); // 通知其它线程把数据出栈
buffer[index] = c; // 数据入栈
index++; // 指针向上移动
}
public synchronized int pop() { // 加上互斥锁
while (index == 0) { // 堆栈无数据,不能出栈
try {
this.wait(); // 等待其它线程把数据入栈
} catch (InterruptedException e) {
}
}
this.notify(); // 通知其它线程入栈
index--; // 指针向下移动
return buffer[index]; // 数据出栈
}
}
class Producer implements Runnable { // 生产者类
SyncStack theStack;
public Producer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
c =0;
theStack.push(c); // 把字符入栈
System.out.println("Produced: " + c); // 打印字符
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
}
class Consumer implements Runnable { // 消费者类
SyncStack theStack;
public Consumer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
c = theStack.pop(); // 从堆栈中读取字符
if(c >=100)
c--;
else if( c<=0 )
c++;
System.out.println("Consumed: " + c);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
}
public class Angry {
public static void main(String args[]) {
SyncStack stack = new SyncStack();
// 下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
Runnable source = new Producer(stack);
Runnable sink = new Consumer(stack);
Thread t1 = new Thread(source); // 线程实例化
Thread t2 = new Thread(sink); // 线程实例化
t1.start(); // 线程启动
t2.start(); // 线程启动
}
}
问题:
1. 使用多线程的意义?
2. 线程两种创建方法的区别?
public class RunableDemo{
public static void main(String[] args){
A a = new A();
a.thead1.start();
a.thead2.start();
}
}
class A implements Runnable{
Thread thead1, thead2;
int x = 50;
A(){
thead1 = new Thread(this);
thead2= new Thread(this);
}
public void run() {
if (Thread.currentThread() == thead1) {
if(x >= 100)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
else
x++;
this.notify();
System.out.println(x);
}
else if (Thread.currentThread() == thead2) {
if(x < 0)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
else
x--;
this.notify();
System.out.println(x);
}
}
}
public class RunableDemo{
public static void main(String[] args){
A a = new A();
a.thead1.start();
a.thead2.start();
}
}
class A implements Runnable{
Thread thead1, thead2;
int x = 50;
A(){
thead1 = new Thread(this);
thead2= new Thread(this);
}
public void run1() {
if(x >= 100)
try {
thead1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
else
x++;
//this.notify();
System.out.println(x);
}
public void run2() {
if(x < 0)
try {
thead2.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
else {
x--;
this.notify();
}
System.out.println(x);
}
}
class thread1 extends Thread{
int x= 10;
public void run1() {
if (x >= 100){
try {
this.wait();
} catch (Exception e) {
}
}
else{
x--;
this.notify();
}
}
class thread2 extends Thread{
int x= 10;
public void run2(){
if (x < 0)
try{this.wait();
}
catch(Exception e){
}
else{
x++;
this.notify();
}
}
public class Demoth {
public static void main(String[] args){
Thread th1 = new Thread();
th1.start();
Thread th2 = new Thread();
th2.start();
}
}
目的与要求:熟悉多线程的创建以及线程的同步。
内容:
编写一个应用程序,除了主线程外,还有两个子线程。两个子线程对同一个数据操作,其中一个线程负责对该数据做递增操作,一个线程负责对该线程做递减操作。当这个数据小于0的话,递减操作等待,当这个数据大于100的话,递增操作等待。
package angry;
class SyncStack { // 同步堆栈类
private int index = 0; // 堆栈指针初始值为0
private int[] buffer = new int[1]; // 堆栈有6个字符的空间
public synchronized void push(int c) { // 加上互斥锁
while (index == buffer.length) { // 堆栈已满,不能压栈
try {
this.wait(); // 等待,直到有数据出栈
} catch (InterruptedException e) {
}
}
this.notify(); // 通知其它线程把数据出栈
buffer[index] = c; // 数据入栈
index++; // 指针向上移动
}
public synchronized int pop() { // 加上互斥锁
while (index == 0) { // 堆栈无数据,不能出栈
try {
this.wait(); // 等待其它线程把数据入栈
} catch (InterruptedException e) {
}
}
this.notify(); // 通知其它线程入栈
index--; // 指针向下移动
return buffer[index]; // 数据出栈
}
}
class Producer implements Runnable { // 生产者类
SyncStack theStack;
public Producer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
c =0;
theStack.push(c); // 把字符入栈
System.out.println("Produced: " + c); // 打印字符
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
}
class Consumer implements Runnable { // 消费者类
SyncStack theStack;
public Consumer(SyncStack s) {
theStack = s;
}
public void run() {
int c;
c = theStack.pop(); // 从堆栈中读取字符
if(c >=100)
c--;
else if( c<=0 )
c++;
System.out.println("Consumed: " + c);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
}
}
}
public class Angry {
public static void main(String args[]) {
SyncStack stack = new SyncStack();
// 下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
Runnable source = new Producer(stack);
Runnable sink = new Consumer(stack);
Thread t1 = new Thread(source); // 线程实例化
Thread t2 = new Thread(sink); // 线程实例化
t1.start(); // 线程启动
t2.start(); // 线程启动
}
}
问题:
1. 使用多线程的意义?
2. 线程两种创建方法的区别?
public class RunableDemo{
public static void main(String[] args){
A a = new A();
a.thead1.start();
a.thead2.start();
}
}
class A implements Runnable{
Thread thead1, thead2;
int x = 50;
A(){
thead1 = new Thread(this);
thead2= new Thread(this);
}
public void run() {
if (Thread.currentThread() == thead1) {
if(x >= 100)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
else
x++;
this.notify();
System.out.println(x);
}
else if (Thread.currentThread() == thead2) {
if(x < 0)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
else
x--;
this.notify();
System.out.println(x);
}
}
}
public class RunableDemo{
public static void main(String[] args){
A a = new A();
a.thead1.start();
a.thead2.start();
}
}
class A implements Runnable{
Thread thead1, thead2;
int x = 50;
A(){
thead1 = new Thread(this);
thead2= new Thread(this);
}
public void run1() {
if(x >= 100)
try {
thead1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
else
x++;
//this.notify();
System.out.println(x);
}
public void run2() {
if(x < 0)
try {
thead2.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
else {
x--;
this.notify();
}
System.out.println(x);
}
}
class thread1 extends Thread{
int x= 10;
public void run1() {
if (x >= 100){
try {
this.wait();
} catch (Exception e) {
}
}
else{
x--;
this.notify();
}
}
class thread2 extends Thread{
int x= 10;
public void run2(){
if (x < 0)
try{this.wait();
}
catch(Exception e){
}
else{
x++;
this.notify();
}
}
public class Demoth {
public static void main(String[] args){
Thread th1 = new Thread();
th1.start();
Thread th2 = new Thread();
th2.start();
}
}