目录
Java高级特性 - 多线程基础(1)使用线程
创建线程
使用 Callable 和 Future 创建线程
Java高级特性 - 多线程基础(2)常用函数
线程的状态与调度 ABC D A B D ABD
常用函数(一)
常用函数(二)
Java高级特性 - 多线程基础(3)线程同步
并发编程的三个概念 ABD AB E
使用synchronized关键字同步线程
使用线程锁(Lock)实现线程同步
使用volatile实现变量的可见性
Java高级特性 - 多线程练习题
顺序输出
售票窗口
如果此文章对你有帮助,麻烦点个赞哦~~~
点个关注,追新不迷路~,下一篇——JAVA数据结构与算法
Java高级特性 - 多线程基础(1)使用线程
创建线程
package step1;
//请在此添加实现代码
/********** Begin **********/
public class ThreadClassOne extends Thread{
public int i=0;
public ThreadClassOne(){
super();
}
public void run(){
for(i=0;i<10;i++){
if(i%2==1){
System.out.print(i+" ");
}
}
}
}
class ThreadClassTwo implements Runnable {
private Thread t;
public void run() {
for (int i = 0; i <= 10; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
}
public void start() {
if (t == null) {
t = new Thread(this, "");
t.start();
}
}
}
/********** End **********/
使用 Callable 和 Future 创建线程
package step2; //声明该类所在的包
import java.util.concurrent.Callable; //引入需要使用的类
import java.util.concurrent.FutureTask;
public class Task { //定义task类
public void runThread(int num) { //定义runThread方法
//请在此添加实现代码
/********** Begin **********/
// 在这里开启线程 获取线程执行的结果
/*这三句代码的作用是创建一个可在另一个线程中执行的任务,并将其封装在一个FutureTask对象中,最后将该FutureTask对象传递给一个新的线程对象,以便在该线程中执行这个任务*/
ThreadCallable t1 = new ThreadCallable(num);
FutureTask<Integer> ft1 = new FutureTask<>(t1);
Thread thread1 = new Thread(ft1,"thread1");
thread1.start(); // 启动线程
try{ // 获取线程执行的结果
System.out.println("线程的返回值为:"+ft1.get());
}catch(Exception e){
e.printStackTrace();
}
/********** End **********/
}
}
//请在此添加实现代码
/********** Begin **********/
/* 在这里实现Callable接口及方法 */
class ThreadCallable implements Callable<Integer> {
int num;
ThreadCallable(int num){
this.num = num;
}
ThreadCallable(){
}
public Integer call() throws Exception{
return getNum(num);
}
private int getNum(int num){
if(num<3){
return 1;
}
else{
return getNum(num-1) +getNum(num-2);
}
}
}
/********** End **********/
Java高级特性 - 多线程基础(2)常用函数
线程的状态与调度 ABC D A B D ABD
常用函数(一)
package step2;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//请在此添加实现代码
/********** Begin **********/
Thread t = new MyThread("子线程",num);
t.start();
/********** End **********/
}
}
//请在此添加实现代码
/********** Begin **********/
class MyThread extends Thread{
private int num;
private String name;
public MyThread(String name,int num){
this.num=num;
this.name=name;
}
public void run(){
int[] arr = new int[2];
arr[0] = 1;
arr[1] = 1;
for (int i = 2; i < num; i++) {
int tmp = arr[1];
arr[1] = arr[0] + arr[1];
arr[0] = tmp;
}
System.out.println("子线程计算结果为:"+arr[1]);
}
}
/********** End **********/
常用函数(二)
package step3;
public class MyThread implements Runnable {
//请在此添加实现代码
/********** Begin **********/
private String name;
private Object prev;
private Object self;
private MyThread(String name,Object prev,Object self){
this.name = name;
this.prev = prev;
this.self = self;
}
public void run(){
int count = 5;
while(count>0){
synchronized(prev){
synchronized(self){
System.out.print(name);
count--;
self.notify();
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.exit(0);
}
public static void main(String[] args) throws Exception {
Object a = new Object();
Object b = new Object();
Object c = new Object();
MyThread ta = new MyThread("E",c,a);
MyThread tb = new MyThread("D",a,b);
MyThread tc = new MyThread("U",b,c);
new Thread(ta).start();
Thread.sleep(100);
new Thread(tb).start();
Thread.sleep(100);
new Thread(tc).start();
Thread.sleep(100);
}
/********** End **********/
}
Java高级特性 - 多线程基础(3)线程同步
并发编程的三个概念 ABD AB E
使用synchronized关键字同步线程
package step2;
public class Task {
public static void main(String[] args) {
final insertData insert = new insertData();
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
}).start();
}
}
}
class insertData {
public static int num = 0;
/********* Begin *********/
public synchronized void insert(Thread thread) {
for (int i = 0; i <= 5; i++) {
num++;
System.out.println(num);
}
}
/********* End *********/
}
使用线程锁(Lock)实现线程同步
package step3;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Task {
public static void main(String[] args) {
final Insert insert = new Insert();
Thread t1 = new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
});
// 设置线程优先级
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
class Insert {
public static int num;
// 在这里定义Lock
private ReentrantLock lock = new ReentrantLock();
public void insert(Thread thread) {
/********* Begin *********/
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + "得到了锁");
for (int i = 0; i < 5; i++) {
num++;
System.out.println(num);
}
} finally {
lock.unlock();
System.out.println(Thread.currentThread().getName() + "释放了锁");
}
/********* End *********/
}
}
使用volatile实现变量的可见性
package step4;
public class Task {
public volatile int inc = 0;
//请在此添加实现代码
/********** Begin **********/
public synchronized void increase() {
inc++;
}
/********** End **********/
public static void main(String[] args) {
final Task test = new Task();
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
for (int j = 0; j < 1000; j++)
test.increase();
};
}.start();
}
while (Thread.activeCount() > 1) // 保证前面的线程都执行完
Thread.yield();
System.out.println(test.inc);
}
}
Java高级特性 - 多线程练习题
顺序输出
package step1;
public class Task {
public static void main(String[] args) throws Exception {
/********* Begin *********/
//在这里创建线程, 开启线程
Object a = new Object();
Object b = new Object();
Object c = new Object();
MyThread ta = new MyThread("A",c,a);
MyThread tb = new MyThread("B",a,b);
MyThread tc = new MyThread("C",b,c);
ta.start();
ta.sleep(100);
tb.start();
tb.sleep(100);
tc.start();
tc.sleep(100);
/********* End *********/
}
}
class MyThread extends Thread {
/********* Begin *********/
private String threadName;
private Object prev;
private Object self;
public MyThread(String name,Object prev,Object self){
this.threadName = name;
this.prev = prev;
this.self = self;
}
public void run() {
int count = 5;
while(count>0){
synchronized(prev){
synchronized(self){
System.out.println("Java Thread"+this.threadName+this.threadName);
count--;
self.notify();
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.exit(0);
}
/********* End *********/
}
售票窗口
package step2;
/********* Begin *********/
//定义站台类,实现卖票的功能。
public class Station extends Thread{
static int tick = 20; // 为了保持票数的一致,票数要静态
static Object ob = new Object(); // 创建一个静态钥匙 值是任意的
public void ticket() {
System.out.println( "卖出了第" + tick + "张票");
tick--;
}
public void run() {
while (tick > 0) {
synchronized (ob) {
if (tick > 0) {
ticket();
}
}
if(tick == 0){
System.out.println("票卖完了");
}
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
}
/********* End *********/
如果此文章对你有帮助,麻烦点个赞哦~~~
点个关注,追新不迷路~,下一篇——JAVA数据结构与算法