Java多线程(头歌)

第1关:创建多线程

代码:

package step1;

 class CreateThreadPractice {

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        // ---------------------Begin------------------------
        //开启线程
        myThread.start();

        // ---------------------End------------------------
    }
}
// ---------------------Begin------------------------
//继承Thread编写名为MyThead的类,代码内容为循环输出10遍: 线程在运行......
class MyThread extends Thread {
    @Override
    public void run() {
        for(int i = 1; i <= 10; ++ i )
        System.out.println("线程在运行......");
    }
}


// ---------------------End------------------------

第2关:使用Callable接口创建多线程 

代码:

package step2;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThread_callable implements Callable<String> {
    private final String threadName;

    public MyThread_callable(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 5; i++) {
            System.out.println(threadName + "的call()方法在运行");
        }
        return threadName + "返回结果:" + threadName;
    }
}

public class CallableExample {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        MyThread_callable myThread1 = new MyThread_callable("thread1");
        MyThread_callable myThread2 = new MyThread_callable("thread2");

        FutureTask<String> ft1 = new FutureTask<>(myThread1);
        FutureTask<String> ft2 = new FutureTask<>(myThread2);

        Thread thread1 = new Thread(ft1);
        Thread thread2 = new Thread(ft2);

        thread1.start();
        thread2.start();

        String result1 = ft1.get();
        String result2 = ft2.get();

        System.out.println(result1);
        System.out.print(result2);
    }
}

第3关:使用Runable接口创建多线程 

 代码:

package step3;
// ---------------------Begin------------------------
//定义一个实现Runnable接口的实现类,类名必须命名为MyThread_runable
//tips: 输出语句为:Thread.currentThread().getName()+"的run()方法在运行"
class MyThread_runable implements Runnable {
    @Override
    public void run() {
        for(int i = 0; i < 3; ++ i )
        System.out.println(Thread.currentThread().getName()+"的run()方法在运行");
    }
}



// ---------------------End------------------------

第4关:多线程中的售票问题 

代码:

package step4;
 
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class SellTicket extends Thread {
    //多线程共享资源,票数为30张
    private static ReentrantLock lock = new ReentrantLock();
	private static int ticket = 30;
 
	@Override
	public void run() {
 
		while (true) {
			try {
				lock.lock();
 
				if (ticket > 0) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e1) {
						// TODO 自动生成的 catch 块
						e1.printStackTrace();
					}
					System.out.println("卖出了第" + ticket + "张票");
					ticket--;
				} else {
					System.out.println("票卖完了");
					System.exit(0);
				}
 
			} finally {
 
				lock.unlock();
 
			}
 
		}
    
    // ---------------------End------------------------
}
 
}
/********* End *********/
 

第5关:知识回顾 

选择题答案是:

A D B

  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FindYou.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值