**
線程編程題目:
**
1.編寫10個線程,第一個線程從1加到10,第二個線程從11加20…第十個線程從91加到100,最後再把10個線程結果相加。
public class Test01 {
public static void main(String[] args) throws InterruptedException {
int result=0;
for (int i = 0; i <10; i++) {
Create10 threadCreate10=new Create10();
threadCreate10.start();
threadCreate10.join();
result += threadCreate10.sum + i*10*10;
System.out.println("第"+(i+1)+"個線程----->"+result);
}
System.out.println(Thread.currentThread().getName()+"----->"+result);
}
}
class Create10 extends Thread{
int sum=0;
public void run() {
for (int i = 1; i <= 10; i++) {
sum+=i;
}
}
}
2.創建三個線程,第一個輸出a,第二個線程輸出b,第三個線程輸出c。
public class ThreadTest {
private static int num=3;
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
String cid = scanner.nextLine();
System.out.print(cid);
while (true) {
if (num>0) {
ThreadJoinTest t1 = new ThreadJoinTest("_A");
ThreadJoinTest t2 = new ThreadJoinTest("_B");
ThreadJoinTest t3 = new ThreadJoinTest("_C");
t1.start();
/**join方法可以傳遞參數,join(10)表示main線程會等待t1線程10毫秒,10毫秒過去後,
* main線程和t1線程之間執行順序由串行執行變爲普通的並行執行
*/
t1.join(10);
t2.start();
t2.join(10);
t3.start();
num=-3;
}
}
}
}
class ThreadJoinTest extends Thread {
public ThreadJoinTest(String name) {
super(name);
}
@Override
public void run() {
System.out.print(this.getName());
}
}
3.寫2個線程,其中一個線程打印1~52,另一個線程打印A~Z,打印順序應該是12A34B56C…5152Z。
public class Test03{
public static void main(String[] args) {
Object obj = new Object();
new Thread(() -> {
synchronized (obj) {
int i = 1;
while (i <= 52) {
System.out.print(i);
i++;
System.out.print(i);
i++;
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(() -> {
synchronized (obj) {
char c = 'A';
while (c <= 'Z') {
System.out.print(c++);
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
4.編程實現如下效果:在主線程 main 中,創建線程 thread1 ,且 main 以及 thread1 都會打印一條輸出,要求每次執行程序時 thread1 的輸出都一定在 main 線程之前,使用語言不限。
public class Test04 {
public static void main(String[] args) throws InterruptedException {
MythreadTest1 mythread=new MythreadTest1();
mythread.start();
mythread.join();
System.out.println("main");
}
}
class MythreadTest1 extends Thread{
public void run() {
System.out.println("thread1");
}
}
5.設計 4 個線程,其中兩個線程每次對 j 增加 1 ,另外兩個線程對 j 每次減少 1. 寫出程序。
public class Test05 {
private static int j=0;//定義變量j
public synchronized static void add(){
j++;//定義同步方法每次只有一個線程對j進行j++操作
}
public synchronized static void dec(){
j--;//定義同步方法每次只有一個線程對j進行j--操作
}
public static void main(String[] args) throws InterruptedException{
for(int i=0;i<2;i++){
new Thread(new Runnable(){//使用匿名內部類進行線程的創建,重寫run()方法,調用add()方法
public void run(){
while(true){
add();
System.out.println(j);
}
}
}).start();
Thread.sleep(1000);
new Thread(new Runnable(){//使用匿名內部類進行線程的創建,重寫run()方法,調用dec()方法
public void run(){
while(true){
dec();
System.out.println(j);
}
}
}).start();
}
}
}
6.生產者與消費者的線程關係
public class Test05 {
public static void main(String[] args) {
//等待喚醒案例
BaoZi bz = new BaoZi();
ChiHuo ch = new ChiHuo("吃貨",bz);
BaoZiPu bzp = new BaoZiPu("包子鋪",bz);
ch.start();
bzp.start();
}
}
//實體類表示:包子的餡、皮、存在狀態
class BaoZi {
String pier ;
String xianer ;
boolean flag = false ;//包子資源 是否存在 包子資源狀態
public String getPier() {
return pier;
}
public void setPier(String pier) {
this.pier = pier;
}
public String getXianer() {
return xianer;
}
public void setXianer(String xianer) {
this.xianer = xianer;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
//消費者
class ChiHuo extends Thread{
private BaoZi bz;
public ChiHuo(String name,BaoZi bz){
super(name);
this.bz = bz;
}
@Override
public void run() {
while(true){
synchronized (bz){
if(bz.flag == false){//沒包子
try {
bz.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("吃貨正在吃"+bz.pier+bz.xianer+"包子");
bz.flag = false;
bz.notify();
}
}
}
}
//生產者
class BaoZiPu extends Thread {
private BaoZi bz;
public BaoZiPu(String name,BaoZi bz){
super(name);
this.bz = bz;
}
@Override
public void run() {
int count = 0; //造包子
while(true){ //同步
synchronized (bz){
if(bz.flag == true){//包子資源 存在
try {
bz.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 沒有包子 造包子
System.out.println("包子鋪開始做包子");
if(count%2 == 0){
// 冰皮 五仁
bz.pier = "冰皮";
bz.xianer = "五仁";
}else{
// 薄皮 牛肉大蔥
bz.pier = "薄皮";
bz.xianer = "牛肉大蔥";
}
count++;
bz.flag=true;
System.out.println("包子造好了:"+bz.pier+bz.xianer);
System.out.println("吃貨來吃吧"); //喚醒等待線程 (吃貨)
bz.notify();
}
}
}
}
7.窗口賣票:三個窗口同時進行賣票
public class Test06 {
public static void main(String[] args) {
Tickets tickets=new Tickets();
Thread t1=new Thread(tickets,"窗口1");
Thread t2=new Thread(tickets,"窗口2");
Thread t3=new Thread(tickets,"窗口3");
t1.start();
t2.start();
t3.start();
}
}
class Tickets implements Runnable{
int tickets=100;
Object obj=new Object();
@Override
public void run() {
while (true) {
synchronized (obj) {
if (tickets>0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String name=Thread.currentThread().getName();
System.out.println(name+"正在賣:"+tickets--);
}
}
}
}
}