利用執行緒模擬乙個簡單的商品生產和消費

2021-10-22 06:51:38 字數 1313 閱讀 5003

首先是商品類,裡面有商品生產和扣庫存的方法:

package com.chen.test.demo3;

/** * @description:

* @author chenjianwen

* @date 2021/3/15

**/public class product

p += 1;

system.out.println("produce = " + p);

notify();

}/**

* 消費商品

* @throws interruptedexception

*/public synchronized void consume() throws interruptedexception

p -= 1;

system.err.println("consume = " + p);

notify();

}}

然後是生產者執行緒和消費者執行緒

package com.chen.test.demo3;

import lombok.sneakythrows;

/** * @description:生產者執行緒

* @author chenjianwen

* @date 2021/3/15

**/public class producethread extends thread

@sneakythrows

@override

public void run()

}}

package com.chen.test.demo3;

import lombok.sneakythrows;

/** * @description:消費者執行緒

* @author chenjianwen

* @date 2021/3/15

**/public class consumerthread extends thread

@sneakythrows

@override

public void run()

}}

啟動類

package com.chen.test.demo3;

/** * @description:

* @author chenjianwen

* @date 2021/3/15

**/public class threadtest

}

乙個簡單的多執行緒

頭部 type tmythread class tthread protected procedure execute override end procedure tmythread.execute begin coinitialize nil 如果不加這句,會提示未呼叫coinitialize ...

c 乙個簡單的執行緒

c 本身並沒有提供任何多執行緒機制,但是在windows下,我們可以呼叫sdk win32 api來編寫多執行緒的程式 msdn中createthread原型 1 2 3 4 5 6 7 8 handlecreatethread lpsecurity attributeslpthreadattrib...

建立乙個簡單的執行緒

handle createthread in opt lpsecurity attributes lpthreadattributes,security attributes 結構指定了這個執行緒的安全屬性,如果填 null 則就以預設的安全描述子建立,並且返回的控制代碼不會被繼承。in size ...