-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDemo.java
More file actions
704 lines (595 loc) · 20.1 KB
/
TestDemo.java
File metadata and controls
704 lines (595 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
import common.ListNode;
import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;
import java.util.Calendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import static org.quartz.CalendarIntervalScheduleBuilder.calendarIntervalSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
public class TestDemo implements Job{
private static Logger log = LoggerFactory.getLogger(TestDemo.class);
private static int count = 0;
static class People{
int age;
String name;
public List<Integer> prv = new ArrayList<Integer>();
public People() {
}
public List<Integer> getPrv() {
return prv;
}
public void setPrv(List<Integer> prv) {
this.prv = prv;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public People(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "People{" +
"age=" + age +
", name='" + name + '\'' +
", prv=" + prv +
'}';
}
}
public void TestPeople(){
People wang = new People(12, "wang");
wang.getPrv().add(14);
People ma = new People(13, "ma");
ma.getPrv().add(10);
List<People> list = new ArrayList<People>();
list.add(wang );
list.add(ma);
Collections.sort(list, new Comparator<People>() {
public int compare(People o1, People o2) {
// return o1.getAge()-o2.getAge();
// return o2.getAge()-o1.getAge();
return o2.getPrv().get(0)-o1.getPrv().get(0);
}
});
System.out.println(list.toString());
}
@Test
public void testDeadLock(){
Object o1 = new Object();
Object o2 = new Object();
Thread t1 = new Thread(
()-> {
synchronized (o1){
try {
System.out.println(Thread.currentThread().getName()+" has got 01");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2){
try {
System.out.println(Thread.currentThread().getName() + " has got 02");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"thread1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (o2){
System.out.println(Thread.currentThread().getName()+" has got 02");
synchronized (o1){
System.out.println(Thread.currentThread().getName() + " has got 01");
}
}
}
},"thread2");
t1.start();
t2.start();
}
@Test
public void testComsumerProducer(){
new Thread(()->{
while (true) {
synchronized (TestDemo.class){
if(count == 5){
try {
System.out.println(Thread.currentThread().getName()+" plate is full,waiting for consuming!");
TestDemo.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
count += 1;
System.out.println(Thread.currentThread().getName()+ " has produced one" +"count=" +count);
TestDemo.class.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"producer").start();
new Thread(()->{
while (true){
synchronized (TestDemo.class){
if(count == 0){
try {
System.out.println(Thread.currentThread().getName()+" plate is empty,waiting for producing!");
TestDemo.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
count -= 1;
System.out.println(Thread.currentThread().getName()+ " has consumed one" +"count=" +count);
TestDemo.class.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"consumer").start();
}
@Test
public void testRemoveItemFromCollection(){
List<String> strings = new ArrayList<>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()){
String next = iterator.next();
if (next.equals("a")){
iterator.remove();
}
}
System.out.println(strings.toString());
}
private static TestDemo instance = null;
public static TestDemo getInstance(){
if(instance == null){
instance = new TestDemo();
return instance;
}
else {
return instance;
}
}
/**
* 定时任务
*/
@Test
public void testQuartz() throws SchedulerException, IOException {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
scheduler.start();
Class clazz = null;
try {
clazz = Class.forName("TestDemo");
JobDetail job = newJob(clazz).withIdentity("TestDemo").build();
CalendarIntervalTrigger tigger = newTrigger().startAt(Calendar.getInstance().getTime())
.withIdentity("TestDemo")
.withSchedule(calendarIntervalSchedule()
.withIntervalInSeconds(5)).build();
scheduler.scheduleJob(job, tigger);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(clazz);
System.in.read();
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello,world,This is my first Quartz");
}
/**
* 线程池
*/
@Test
public void testThreadPool(){
}
private class TaskInfo {
private String Id; //主键
private String appId; //应用ID
private String appName; //应用名
private String taskName; //任务名称
private Date startTime; //定时任务开始时间
private Date endTime; //定时任务结束时间
private String intervalTime; //定时任务间隔时间
private String intervalUnit; //定时任务间隔单元
private String operateUrl; //任务操作URL
private String operateClass; //任务操作类
private Date lastRunTime; //定时任务上次执行的时间
private String memo; //备注
private String creator; //创建者账号
private Date createTime;//创建日期
private Date updateTime;//最后更新时间
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getIntervalTime() {
return intervalTime;
}
public void setIntervalTime(String intervalTime) {
this.intervalTime = intervalTime;
}
public String getIntervalUnit() {
return intervalUnit;
}
public void setIntervalUnit(String intervalUnit) {
this.intervalUnit = intervalUnit;
}
public String getOperateUrl() {
return operateUrl;
}
public void setOperateUrl(String operateUrl) {
this.operateUrl = operateUrl;
}
public String getOperateClass() {
return operateClass;
}
public void setOperateClass(String operateClass) {
this.operateClass = operateClass;
}
public Date getLastRunTime() {
return lastRunTime;
}
public void setLastRunTime(Date lastRunTime) {
this.lastRunTime = lastRunTime;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
// @Test
// public Calendar futureDate(Calendar calendar, TaskInfo taskInfo, int unit){
// Calendar instance = Calendar.getInstance();
// instance.setLenient(true);
// return calendar;
// }
// public static void main(String[] args) {
// /**
// * testComsumerProducer
// */
//// TestDemo demo = new TestDemo();
////// demo.testComsumerProducer();
//// ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(2);
//// executor.execute();
// // getInstance();
//// try {
//// demo.testQuartz();
//// } catch (SchedulerException e) {
//// e.printStackTrace();
//// }
// int a = 2;
// int b =-5;
//// System.out.println(Integer.toBinaryString(b));
//// System.out.println(Integer.toBinaryString(a));
//// float c = 0.33f;
//// System.out.println(Power(a,b));
//
// int abs = Math.abs(b);
// abs = abs>>1;
// System.out.println(abs);
//
// }
public static double Power(double base, int exponent) {
if(exponent==0)return 1.0;
else if(exponent == 1)return base;
if(base == 0)return 0;
float res = 1.0f;
for(int i=0,n = Math.abs(exponent); i<n; i++){
res *= base;
}
if(exponent<0)return 1.0/res;
return res;
// long p = Math.abs((long)exponent);
// double r = 1.0;
// while(p != null){
// if(p & 1) r *= base;
// base *= base;
// p >>= 1;
// }
// return exponent < 0 ? 1/ r : r;
}
@Test
public void testHashMap_HashTable(){
Map<String, String> map = new HashMap<String, String>();//HashMap对象
Map<String, String> tableMap = new Hashtable<String, String>();//HashTable对象
map.put(null, null);
map.put(null,"3");
System.out.println("hashMap的[key]和[value]均可以为null:" + map.get(null));
// try {
// tableMap.put(null, "3");
// System.out.println(tableMap.get(null));
// } catch (Exception e) {
// System.out.println("【ERROR】:hashTable的[key]不能为null");
// }
// try {
// tableMap.put("3", null);
// System.out.println(tableMap.get("3"));
// } catch (Exception e) {
// System.out.println("【ERROR】:hashTable的[value]不能为null");
// }
}
@Test
public void testPriorityQueue(){
Queue<Integer> queue = new PriorityQueue<>();
queue.offer(3);
queue.offer(2);
queue.offer(1);
queue.poll();
queue.offer(5);
queue.poll();
queue.offer(6);
queue.poll();
queue.offer(4);
queue.poll();
System.out.println(Arrays.toString(queue.toArray()));
}
@Test
public void testArraysToString(){
// ArrayList<Integer> list = new ArrayList<>();
// list.add(3);
// list.add(4);
// HashSet<String> set = new HashSet<>();
// System.out.println(list.toString());
// if(set.contains(list.toString())){
// System.out.println(true);
// }else {
// set.add(list.toString());
// System.out.println(set.contains(list.toString()));
// }
HashSet<Integer> set = new HashSet<>();
set.add(1);
set.add(3);
Integer [] arr = new Integer[3];
set.toArray(arr);
int[] ints = set.stream().mapToInt(Integer::intValue).toArray();
// System.out.println(Arrays.toString(ints));
Integer[] s =null;
s = Arrays.copyOf(arr,arr.length);
System.out.println(s.length);
s = Arrays.copyOfRange(arr,0,2);
System.out.println(s.length);
}
// class Solution {
// public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// ListNode res = l1;
// int carry = 0;
// while (l1 != null && l2 != null) {
// int sum = l1.val + l2.val + carry;
// l1.val = sum % 10;
// carry = sum / 10;
// if(l1.next == null){
// if(l2.next == null){
// if(carry>0){
// l1.next = new ListNode(carry);
// carry = 0;
// }
// }else {
// l1.next = l2.next;
// l1 = l1.next;
// break;
// }
// }
// l1 = l1.next;
// l2 = l2.next;
// }
// while (l1 != null){
// int sum = l1.val + carry;
// l1.val = sum % 10;
// carry = sum / 10;
// if(l1.next == null && carry>0){
// l1.next = new ListNode(carry);
// carry = 0;
// }
// l1 = l1.next;
// }
// return res;
// }
// }
// @Test
// public void testLongest(){
// ListNode l1 = new ListNode(5);
// ListNode l2 = new ListNode(5);
// l2.next = new ListNode(8);
//
// Solution solution = new Solution();
// ListNode listNode = solution.addTwoNumbers(l1, l2);
// System.out.println();
// }
@Test
public void testNewArrayList(){
int[] a = {1,2,3};
// Integer [] integers = Arrays.stream(a).boxed().toArray(Integer[]::new);
// System.out.println(Arrays.asList(integers));
}
// class Solution {
// private HashSet<String> set = new HashSet<String>();
// private List<List<Integer>> res = new ArrayList<>();
// private List<Integer> tmp = null;
// private boolean [] visit = null;
// public List<List<Integer>> permuteUnique(int[] nums) {
// if(nums != null){
// Arrays.sort(nums);
// visit= new boolean[nums.length];
// backtrack(nums, nums.length, 0);
// }
// return res;
// }
//
// public void swap(int [] nums, int i, int j){
// int tmp = nums[i];
// nums[i] = nums[j];
// nums[j] = tmp;
// }
//
// public void backtrack(int [] nums, int n, int first){
// if(n == first){
// tmp = new ArrayList<>(n);
// for (int num : nums) {
// tmp.add(num);
// }
// res.add(tmp);
// }
// Set<Integer> set = new HashSet<Integer>();
// for(int i = first; i< n;i++){
// if(set.contains(nums[i])){
// continue;
// }else{
// set.add(nums[i]);
// }
// swap(nums, i, first);
// backtrack(nums, n, first+1);
// swap(nums, i, first);
// }
// }
// }
// @Test
// public void testBackTrack(){
// Solution solution = new Solution();
// int []a = {1,1,2};
// List<List<Integer>> permute = solution.permuteUnique(a);
// System.out.println(permute.toString());
// }
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return null;
}
ListNode p= headA,q = headB;
while ((p!=null && p.next != null )||( q != null && q.next != null)){
p = p.next;
q = q.next;
}
if(p.next == null){
p.next = headB;
}
if(q.next == null){
q.next = headA;
}
while (q != null || p!=null ){
if(q.next == null){
q.next = headA;
}
if(p.next == null){
p.next = headB;
}
if(p.val == q.val){
return q;
}
p = p.next;
q = q.next;
}
return null;
}
}
// @Test
// public void testLongest(){
// ListNode l1 = new ListNode(4);
// l1.next = new ListNode(1);
// l1.next.next = new ListNode(8);
// l1.next.next.next = new ListNode(4);
// l1.next.next.next.next = new ListNode(5);
// ListNode l2 = new ListNode(1);
// l2.next = new ListNode(5);
// l2.next.next = new ListNode(0);
// l2.next.next.next = new ListNode(1);
// l2.next.next.next.next = new ListNode(8);
// l2.next.next.next.next.next = new ListNode(4);
// l2.next.next.next.next.next.next = new ListNode(6);
//
// Solution solution = new Solution();
// ListNode listNode = solution.getIntersectionNode(l1, l2);
// System.out.println();
// }
/**
* 超时ArrayList的byg
*/
@Test
public void initByCollection(){
//Arrays.asList()返回的是具体的类型
List<String> list = Arrays.asList("hello,world");
Object[] objects = list.toArray();
log.info(objects.getClass().getSimpleName());//===> String[]
//objects 元素的类型是String,存储Object就会报错,因为JVM不清楚你存储的
objects[0] = new Object();
}
}