package java_basic;
public class java_Thread_1 {
/*
*
* Thread的sleep()、join()和wait()
*
*
*
*/
// 定义锁
private static final Object LOCK = new Object();
public static void main(String[] args) throws Exception {
// System.out.println("sleep:");
// fun1();
// Thread.sleep(5000);
//
// System.out.println("join:");
// fun2();
// Thread.sleep(5000);
// System.out.println("wait:");
fun3();
}//main
// fun3 wait
private static void fun3() throws Exception {
/*
*
*
* 运行结果:线程2运行结束后,由于线程1没有被唤醒,所以一直阻塞在那里.....
可以使用带有参数的wait(long mills),当等待的时间结束后,线程会被自动唤醒
*
* */
Thread thread1 = new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
System.out.println("线程1抢到了锁....");
this.wait();/
System.out.println("线程1运行结束.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public synchronized void run() {
System.out.println("线程2抢到了锁....");
System.out.println("线程2运行结束.....");
}
});
thread1.start();//开启线程1
//让主线程稍等片刻,确保线程1已经运行
Thread.sleep(200);
thread2.start();//开启线程2
}//m3
// fun2 join
public static void fun2() throws Exception {
/*
*
* 线程2先启动,当运行到thread1.join()时,线程2停止运行,等待线程1执行结束,
* 虽然线程1启动比线程2迟,但是只有当线程1运行结束后,线程2才能继续运行。
*
* */
final Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("线程1开始运行.....");
// 为了验证线程1在线程2之前运行结束,这里让线程1睡眠3秒
Thread.sleep(3000);
System.out.println("线程1运行结束.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("线程2开始运行.....");
thread1.join();/// 线程2运行到这里会等待线程1运行结束
System.out.println("线程2运行结束.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread2.start();// 先开启线程2
// Thread.sleep(200);
Thread.sleep(1000);
thread1.start();// 在开启线程1
}//m
// fun1 sleep
public static void fun1() throws InterruptedException {
Thread thread1 = new Thread(new Runnable() {
/*
*
从运行的结果可以看到,线程1先开始执行,在线程1拿到锁后,线程2也开始执行,
但是此时是线程1持有锁,所以线程2阻塞,当线程1开始睡眠时,线程2还是没有拿到锁,
直到线程1运行结束释放了锁后,线程2才继续运行。
所以,sleep()不释放锁。
所以,sleep()不释放锁。
所以,sleep()不释放锁。
所以,sleep()不释放锁。
*/
@Override
public void run() {
System.out.println("线程1开启运行....");
synchronized (LOCK) {
try {
System.out.println("线程1抢到了锁....");
Thread.sleep(2000);//
System.out.println("线程1运行结束.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("线程2开启运行....");
synchronized (LOCK) {
System.out.println("线程2抢到了锁....");
System.out.println("线程2运行结束.....");
}
}
});
thread1.start();// 开启线程1
// 让主线程稍等片刻,确保线程1已经运行
Thread.sleep(300);///
thread2.start();// 开启线程2
}
}//c
----------------------------------迭代器-------------------------------
package java_basic;
import java.util.*;
import java.util.Map.Entry;
public class java_迭代器Iterator {
public static void main(String[] args) {
// iterator_list();
// iterator_set();
iterator_map();
}//main
public static void iterator_map() {
Map<Integer,String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
//1----
//----所有键值对中的键,组成一个集合
Set<Integer > set = map.keySet();
Iterator<Integer> iter = set.iterator();
while(iter.hasNext()){
System.out.println(iter.next());//打印出map中的键(1,2,3)
}
// -------打印出值
//values所有的值组成的一个集合
Collection<String> col = map.values();
//重写了toString方法
System.out.println(col);//打印出a,b,c的值
// values() 返回set集合
// keySets() 返回collection
// 2---------------entrySet()
Set<Map.Entry<Integer, String>> entrySet=map.entrySet();
Iterator<Map.Entry<Integer, String>> iter1=entrySet.iterator();
while(iter1.hasNext()) {
Map.Entry<Integer, String> entry=iter1.next();
System.out.print("键:"+entry.getKey());
System.out.println("\t\t值:"+entry.getValue());
}
System.out.println("entrySet简化方法:(用的多)");
for(Entry<Integer,String> entry : map.entrySet() ){
int key=entry.getKey();
String s=entry.getValue();
System.out.println("--> "+key+" "+s);
}
}//m
public static void iterator_set() {
//存储数据的地址
Set<String> set = new HashSet<>();
//存储数据
set.add("dfew");
set.add( "78678");
set.add( "2323jijij8");
//遍历数据
Iterator<String> iter = set.iterator();
while(iter.hasNext()){
String s= iter.next();
System.out.println(s);
}
}
public static void iterator_list() {
List<Integer> list = new ArrayList<>();
//集合
list.add(10);
list.add(2);
list.add(30);
list.add(3);
list.add(39);
list.add(31);
//Iterator迭代器
//1、获取迭代器
Iterator<Integer> iter = list.iterator();
//2、通过循环迭代
//hasNext():判断是否存在下一个元素
while(iter.hasNext()){
//如果存在,则调用next实现迭代
//Object-->Integer-->int
// int j=(int)iter.next(); //把Object型强转成int型
int j=iter.next(); //
System.out.println(j);
}
}
}