Commit ce39c6d9 by shuhu.hou

init

parent b16f7cb9
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-training</artifactId>
<groupId>cn.freemud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo1</artifactId>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package cn.freemud.demo.po;
import com.alibaba.fastjson.JSON;
/**
* Created by chenwenshun on 2018/9/27.
*/
public class User {
private Long id;
private String username;
Integer age;
public User(Long id, String username) {
this.id = id;
this.username = username;
}
public User(Long id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
package cn.freemud.demo.zookeeper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws Exception {
System.out.println( "Hello World!" );
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
client.start();
// client.create().forPath("/ashun");
final int count = 0;
final InterProcessMutex lock = new InterProcessMutex(client, "/ashun/LOCK");
final CountDownLatch began = new CountDownLatch(1);
//region 启动线程
new Thread(new Runnable() {
private int flag = 1;
public void run() {
try {
began.await();
if ( lock.acquire(3, TimeUnit.SECONDS) )
{
try
{
// do some work inside of the critical section here
System.out.println("Thread :"+Thread.currentThread()+" running...."+flag);
}
finally
{
lock.release();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//endregion
//region 启动线程
new Thread(new Runnable() {
public void run() {
try {
began.await();
if ( lock.acquire(3, TimeUnit.SECONDS) )
{
try
{
// do some work inside of the critical section here
System.out.println("Thread :"+Thread.currentThread()+" running....");
}
finally
{
lock.release();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//endregion
//region 启动线程
new Thread(new Runnable() {
public void run() {
try {
began.await();
if ( lock.acquire(3, TimeUnit.SECONDS) )
{
try
{
// do some work inside of the critical section here
System.out.println("Thread :"+Thread.currentThread()+" running....");
}
finally
{
lock.release();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//endregion
//region 启动线程
new Thread(new Runnable() {
public void run() {
try {
began.await();
if ( lock.acquire(3, TimeUnit.SECONDS) )
{
try
{
// do some work inside of the critical section here
System.out.println("Thread :"+Thread.currentThread()+" running....");
}
finally
{
lock.release();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//endregion
//region 启动线程
new Thread(new Runnable() {
public void run() {
try {
began.await();
if ( lock.acquire(3, TimeUnit.SECONDS) )
{
try
{
// do some work inside of the critical section here
System.out.println("Thread :"+Thread.currentThread()+" running....");
}
finally
{
lock.release();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//endregion
//region 启动线程
new Thread(new Runnable() {
public void run() {
try {
began.await();
if ( lock.acquire(3, TimeUnit.SECONDS) )
{
try
{
// do some work inside of the critical section here
System.out.println("Thread :"+Thread.currentThread()+" running....");
}
finally
{
lock.release();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//endregion
began.countDown();
// client.close();
}
public class Runner implements Runnable{
public void run() {
}
}
}
package cn.freemud.demo.zookeeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.CountDownLatch;
/**
* Created by chenwenshun on 2017/7/9.
*/
public class App2 {
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application.xml");
CuratorFramework client = (CuratorFramework)applicationContext.getBean("curatorFramework");
// RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
// CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
// client.start();
// client.create().forPath("/ashun");
try {
client.getChildren().usingWatcher(new Watcher() {
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!");
}
}).forPath("/ashun/LOCK");
} catch (Exception e) {
e.printStackTrace();
}
// InterProcessMutex lock = new InterProcessMutex(client, "/ashun/LOCK");
CountDownLatch began = new CountDownLatch(1);
Q q = new Q(0);
for (int i = 0; i < 10; i++) {
InterProcessMutex lock = new InterProcessMutex(client, "/ashun/LOCK");
// new Thread(new Runner(lock, began, q), "thread_"+i).start();
new Thread(new Runner2(began, q), "thread_"+i).start();
}
// client.setData().forPath("/ashun/LOCK","haha".getBytes());
began.countDown();
System.out.println( "Hello World!" );
}
}
package cn.freemud.demo.zookeeper;
/**
* Created by chenwenshun on 2017/7/9.
*/
public class Q {
private volatile int n;
public Q(int n) {
this.n = n;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}
package cn.freemud.demo.zookeeper;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Created by chenwenshun on 2017/7/9.
*/
public class Runner implements Runnable {
private InterProcessMutex lock;
private CountDownLatch began;
private Q q;
public Runner(InterProcessMutex lock, CountDownLatch began, Q q) {
this.lock = lock;
this.began = began;
this.q = q;
}
public void run() {
try {
began.await();
boolean getLock = false;
try
{
getLock = lock.acquire(11, TimeUnit.SECONDS);
if (getLock){
// do some work inside of the critical section here
TimeUnit.SECONDS.sleep(1);
q.setN(q.getN()+1);
System.out.println("Thread :"+Thread.currentThread()+" running.... >>>"+q.getN());
}else{
System.out.println("Thread :"+Thread.currentThread()+" You do not own the lock.... >>>");
}
}
finally
{
// if (getLock)
lock.release();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
package cn.freemud.demo.zookeeper;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Created by chenwenshun on 2017/7/9.
*/
public class Runner2 implements Runnable {
private CountDownLatch began;
private Q q;
public Runner2(CountDownLatch began, Q q) {
this.began = began;
this.q = q;
}
public void run() {
try {
began.await();
TimeUnit.SECONDS.sleep(3);
q.setN(q.getN()+1);
System.out.println("Thread :"+Thread.currentThread()+" running.... >>>"+q.getN());
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!--<task:annotation-driven/> -->
<context:component-scan base-package="com" use-default-filters="false">
<!-- 扫描符合@Service @Repository的类 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
<!-- 重连策略 -->
<bean id="retryPolicy" class="org.apache.curator.retry.ExponentialBackoffRetry">
<!-- 间隔时间基数 -->
<constructor-arg index="0" value="1000" />
<!-- 重连策略 -->
<constructor-arg index="1" value="3000" />
</bean>
<bean id="curatorFramework" class="org.apache.curator.framework.CuratorFrameworkFactory" factory-method="newClient" init-method="start">
<constructor-arg index="0" value="localhost:2181" />
<!--<constructor-arg index="0" value="server1:port,server2:port,server3:port" />-->
<!-- sessionTimeoutMs会话超时时间,单位为毫秒。默认是60000ms -->
<constructor-arg index="1" value="5000" />
<!-- connectionTimeoutMs连接创建超时时间,单位毫秒,默认15000ms -->
<constructor-arg index="2" value="3000" />
<constructor-arg index="3" ref="retryPolicy" />
</bean>
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context"
name="springAppName"
source="spring.application.name"/>
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d %p (%file:%line\)- ${springAppName:-} %X{X-B3-TraceId:-} %X{X-B3-SpanId:-} %m%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
\ No newline at end of file
package cn.freemud.demo;
import cn.freemud.demo.po.User;
import cn.freemud.demo.zookeeper.Q;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import com.google.common.util.concurrent.RateLimiter;
import org.junit.Test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created by chenwenshun on 2018/9/27.
*/
public class GuavaTests {
@Test
public void testSplit(){
String mock = ",,a, b,c,,d,,";
String[] mocks = mock.split(",");
for (int i = 0; i < mocks.length; i++) {
System.out.println(mocks[i]);
}
List<String> list = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(mock);
System.out.println(list.toString());
Joiner joiner = Joiner.on("/").skipNulls();
String result = joiner.join("a", 1, null, 'c');
System.out.println(result);
String[] names = new String[]{"bella", "tina", "ashun"};
System.out.println(joiner.join(names));
List<String> time = Splitter.on(":").splitToList("10:56");
System.out.println(time);
}
@Test
public void testRateLimite(){
RateLimiter limiter = RateLimiter.create(3);
// RateLimiter limiter = RateLimiter.create(10, 5, TimeUnit.SECONDS);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
for (; ; ) {
limiter.acquire();
System.out.println(df.format(new Date()));
}
}
@Test
public void testMultimap(){
ListMultimap<Integer, String> listMutilMap = ArrayListMultimap.create();
listMutilMap.put(100, "1001");
listMutilMap.put(2, "21");
listMutilMap.put(2, "22");
listMutilMap.put(1, "1");
listMutilMap.put(1, "2");
listMutilMap.put(43, "431");
System.out.println(listMutilMap.keySet());//
System.out.println(listMutilMap.get(2));
}
/**
* 不可变集合
*/
@Test
public void test4(){
List<String> list = Lists.newArrayList("a", "b", "c");
Collection<String> unmodifiableCollection = Collections.unmodifiableCollection(list);
ImmutableList<String> immutableList = ImmutableList.copyOf(list);
list.add("e");
System.out.println(unmodifiableCollection);
System.out.println(immutableList);
immutableList.add("a");
}
/**
* 集合 索引
*/
@Test
public void test5(){
List<User> users = Lists.newArrayList();
users.add(new User(1L,"haha"));
users.add(new User(2L,"bella"));
users.add(new User(3L,"tina"));
users.add(new User(4L,"tom"));
users.add(new User(5L,"jerry"));
users.add(new User(6L,"tony"));
users.add(new User(7L,"tony"));
/**
* 对集合添加唯一索引
*/
ImmutableMap<Long, User> uniqueIndex = FluentIterable
.from(users).uniqueIndex(new Function<User, Long>() {
@Override
public Long apply(User user) {
return user.getId();
}
});
User user = uniqueIndex.get(3L);
System.out.println(user);
/**
* 对集合添加非唯一索引
*/
ImmutableListMultimap<String, User> index = FluentIterable
.from(users).index(new Function<User,String>() {
@Override
public String apply(User user) {
return user.getUsername();
}
});
List<User> userList = index.get("tony");
System.out.println(userList);
}
/**
* 集合运算主要有:并集、交集、差集。
*/
@Test
public void test6() {
List<Integer> list1 = Lists.newArrayList(1, 2, 2, 1, 4, 5, 4, 3);
List<Integer> list2 = Lists.newArrayList(1, 2, 3, 7, 8, 9);
Set set = new HashSet(list1);
Sets.SetView<Integer> union = Sets.union(Sets.newHashSet(list1), Sets.newHashSet(list2));
Sets.SetView<Integer> difference = Sets.difference(Sets.newHashSet(list1), Sets.newHashSet(list2));
Sets.SetView<Integer> intersection = Sets.intersection(Sets.newHashSet(list1), Sets.newHashSet(list2));
System.out.println(union);
System.out.println(difference);
System.out.println(intersection);
list1.parallelStream().forEach(System.out::println);
list1.stream().forEach(System.out::println);
int a = list1.stream().max(Comparator.comparing(e -> e)).get();
int sum = list1.stream().mapToInt(e -> e).sum();
System.out.println(sum);
}
/**
* java 8 Stream
*/
@Test
public void test7(){
List<User> users = Lists.newArrayList();
users.add(new User(1L,"haha"));
users.add(new User(2L,"bella"));
users.add(new User(3L,"tina"));
users.add(new User(4L,"tom"));
users.add(new User(5L,"jerry"));
users.add(new User(6L,"tony"));
users.add(new User(7L,"tony"));
List<Q> names = users.parallelStream()
// .filter(u -> u.getUsername().equals("tony"))
// .sorted(Comparator.comparing(User::getId).reversed())
.sorted((user1,user2) -> user1.getId().compareTo(user2.getId()))
.map(user -> {
Q q = new Q(user.getId().intValue());
return q;
})
.collect(Collectors.toList());
System.out.println(names);
Map<Long,User> userMap = users.stream().collect(Collectors.toMap(user -> user.getId(),user -> user)) ;
System.out.println(userMap);
/**
* 非唯一索引
*/
Map<Integer, List<User>> integerListMap =
Stream.generate(new UserSupplier())
.limit(100)
.collect(Collectors.groupingBy(User::getAge));
System.out.println(integerListMap);
}
private class UserSupplier implements Supplier<User>{
private long index = 0;
private Random random = new Random();
@Override
public User get() {
return new User(index++, "StormTestUser"+random.nextInt(1000), random.nextInt(100));
}
}
}
......@@ -10,13 +10,18 @@
<packaging>pom</packaging>
<modules>
<module>demo1</module>
<module>test01</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-training</artifactId>
<groupId>cn.freemud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test01</artifactId>
</project>
\ No newline at end of file
this is Writer
\ No newline at end of file
package cn.freemud.test01.tranning;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class HashMapTest {
/* form格式请求,按字母顺排序进行加签
key1=value1&key2=value2*/
@Test
public void test1() {
Map<String, String> maps = getMaps();
StringBuilder sb=new StringBuilder();
for (String key:maps.keySet()) {
sb.append(key);
sb.append("=");
sb.append(maps.get(key));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb.toString());
}
// 获取map
private Map<String, String> getMaps() {
Map<String, String> map = new HashMap<>();
map.put("b", "1y78");
map.put("k", "fjaj");
map.put("g", "fafa");
map.put("a", "85543538");
return map;
}
}
package cn.freemud.test01.tranning;
import org.junit.Test;
import java.io.*;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class IOTest {
//字节流
@Test
public void Test1() {
File f = new File("src/main/resources/1.txt");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(f);
String s = "this is outputStream";
outputStream.write(s.getBytes());
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void Test2() {
File f = new File("src/main/resources/1.txt");
Writer out = null;
try {
out = new FileWriter(f);
String str = "this is Writer";
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package cn.freemud.test01.tranning;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class ListTest {
//ArrayList与LinkedList,对比在列表中间的部分添加或删除数据性能
@Test
public void Test() {
Random random = new Random();
ArrayList<String> arrayList = getArrayList();
long begin_arrayList = System.currentTimeMillis();
for (Integer i=0;i<5000;i++){
arrayList.add(1000,i.toString()+"a");
}
System.out.println(System.currentTimeMillis()-begin_arrayList);
LinkedList<String> linkedList = getLinkedList();
long begin_linkedList = System.currentTimeMillis();
for (Integer i=0;i<5000;i++){
linkedList.add(1000,i.toString()+"a");
}
System.out.println(System.currentTimeMillis()-begin_linkedList);
}
private ArrayList<String> getArrayList(){
ArrayList<String> list=new ArrayList<String>();
for (Integer i=0;i<1000000;i++){
list.add(i.toString());
}
return list;
}
private LinkedList<String> getLinkedList(){
LinkedList<String> list=new LinkedList<String>();
for (Integer i=0;i<1000000;i++){
list.add(i.toString());
}
return list;
}
}
package cn.freemud.test01.tranning;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class RunnableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//设计三个线程
MyThread1 mt = new MyThread1();
Thread t1 = new Thread(mt, "一号窗口");
Thread t2 = new Thread(mt, "二号窗口");
Thread t3 = new Thread(mt, "三号窗口");
t1.start();
t2.start();
t3.start();
}
}
class MyThread1 implements Runnable{
private int ticket =10;
private String name;
public void run(){
for(int i =0;i<500;i++){
if(this.ticket>0){
System.out.println(Thread.currentThread().getName()+"卖票---->"+(this.ticket--));
}
}
}
}
package cn.freemud.test01.tranning;
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class SetTest {
//简单key value去重输出
@Test
public void Test(){
Map<String, String> maps = getMaps();
Set<Map.Entry<String, String>> entries = maps.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, String> value = iterator.next();
System.out.println(value.getKey()+"------>"+value.getValue());
}
}
//对象去重输出
@Test
public void Test2(){
Map<String, User> maps = getUserMaps();
Set<Map.Entry<String, User>> entries = maps.entrySet();
Iterator<Map.Entry<String, User>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, User> value = iterator.next();
System.out.println(value.getKey()+"------>"+value.getValue());
}
}
// 获取map
private Map<String, String> getMaps() {
Map<String, String> map = new HashMap<>();
map.put("b", "1y78");
map.put("b", "1y78");
map.put("k", "fjaj");
map.put("g", "fafa");
map.put("a", "85543538");
return map;
}
private Map<String,User> getUserMaps(){
Map<String,User> maps=new HashMap<>();
User user = new User();
user.setAge(10);
user.setName("tony");
maps.put("001",user);
user.setAge(10);
user.setName("tony");
maps.put("001",user);
user.setAge(15);
user.setName("lucy");
maps.put("003",user);
return maps;
}
}
package cn.freemud.test01.tranning;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class ThreadTest {
public static void main(String[] args) {
MyThread mt1= new MyThread("一号窗口");
MyThread mt2= new MyThread("二号窗口");
MyThread mt3= new MyThread("三号窗口");
mt1.start();
mt2.start();
mt3.start();
}
}
class MyThread extends Thread {
private int ticket = 10;
private String name;
public MyThread(String name) {
this.name = name;
}
public void run() {
for(int i =0;i<10;i++){
synchronized (this){
if(this.ticket>0){
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+"卖票---->"+(this.ticket--));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
package cn.freemud.test01.tranning;
/**
* All rights Reserved, Designed By www.freemud.com
*
* @version V1.0
* @Title:
* @Package: cn.freemud.test01.tranning
* @Descripttion:
* @author: shuhu.hou
* @date: 2018/11/1
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目.
*/
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
User user = (User) obj;
return (user.age.toString()+user.name).equals(this.age.toString()+this.name);
}
@Override
public String toString() {
return this.age.toString()+this.name;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment