码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA进阶14

时间:2019-03-29 01:19:28      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:进度   extends   rar   tac   alt   常用   info   exception   java.awt   

间歇性混吃等死,持续性踌躇满志系列-------------第14天

1、线程的加入

技术图片
 1 package code0328;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JoinTest extends JFrame {
 7     //定义两个线程
 8     private Thread threadA;
 9     private Thread threadB;
10     //定义两个进度条组件
11     final JProgressBar progressBar = new JProgressBar();
12     final JProgressBar progressBar2 = new JProgressBar();
13     int count = 0;
14 
15     public static void main(String[] args) {
16         init(new JoinTest(), 400, 400);
17     }
18 
19     public JoinTest() {
20         super();
21         //将进度条设置在窗体最北面
22         getContentPane().add(progressBar, BorderLayout.NORTH);
23         //将进度条设置在窗体最南面
24         getContentPane().add(progressBar2, BorderLayout.SOUTH);
25         progressBar.setStringPainted(true);
26         progressBar2.setStringPainted(true);
27         //使用匿名内部类形式初始化Thread实例
28         threadA = new Thread(new Runnable() {
29             int count = 0;
30 
31             //重写run方法
32             public void run() {
33                 while (true) {
34                     //设置进度条的当前值
35                     progressBar.setValue(++count);
36                     try {
37                         //使A休眠0.1秒
38                         threadA.sleep(100);
39                         //使B调用join()方法
40                         threadB.join();
41                     } catch (Exception e) {
42                         e.printStackTrace();
43                     }
44                 }
45             }
46         });
47         //启动线程A
48         threadA.start();
49         threadB = new Thread(new Runnable() {
50             public void run() {
51                 //设置进度条的当前值
52                 progressBar2.setValue(++count);
53                 try {
54                     //使B休眠0.1秒
55                     threadB.sleep(100);
56                 } catch (Exception e) {
57                     e.printStackTrace();
58                 }
59                 //当count变量增长为100时
60 
61             }
62         });
63         threadB.start();
64     }
65 
66     public static void init(JFrame frame, int width, int height) {
67         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68         frame.setSize(width, height);
69         frame.setVisible(true);
70     }
71 }
View Code

运行结果图

技术图片

2、线程的安全

技术图片
 1 package code0328;
 2 
 3 public class ThreadSafeTest implements Runnable {
 4     //设置当前总票数
 5     int num = 10;
 6     public void run(){
 7         while (true){
 8             synchronized (""){
 9                 if (num > 0) {
10                     try {
11                         Thread.sleep(100);
12                     } catch (Exception e) {
13                         e.printStackTrace();
14                     }
15                     System.out.println("tickets:" + --num);
16                 }
17             }
18 
19         }
20     }
21 
22     public static void main(String[] args) {
23         //实例化类对象
24         ThreadSafeTest t = new ThreadSafeTest();
25         //以该类对象分别实例化4个线程
26         Thread TA=new Thread(t);
27         Thread TB=new Thread(t);
28         Thread TC=new Thread(t);
29         Thread TD=new Thread(t);
30         Thread TE=new Thread(t);
31         Thread TF=new Thread(t);
32         //分别启动线程
33         TA.start();
34         TB.start();
35         TC.start();
36         TD.start();
37         TE.start();
38         TF.start();
39     }
40 }
View Code

运行结果图

技术图片

3、 线程的插队运行

技术图片
 1 package code0328;
 2 
 3 class EmergencyThread implements Runnable {
 4     public void run() {
 5         for (int i = 0; i < 6; i++) {
 6             try {
 7                 //当前线程休眠0.1秒实现动态更新
 8                 Thread.sleep(100);
 9             } catch (InterruptedException e) {
10                 e.printStackTrace();
11             }
12             //紧急情况下车辆发车
13             System.out.println("紧急情况:" + i + "号车出发!");
14         }
15     }
16 }
17 
18 public class JoinThread {
19     public static void main(String[] args) {
20         //创建新线程
21         Thread thread = new Thread(new EmergencyThread());
22         //运行新线程
23         thread.start();
24         for (int i = 0; i < 6; i++) {
25             try {
26                 //当前线程休眠0.1秒实现动态更新
27                 Thread.sleep(100);
28             } catch (InterruptedException e) {
29                 e.printStackTrace();
30             }
31             //正常情况下车辆出发
32             System.out.println("正常情况:" + i + "号车出发!");
33             try{
34                 //使用join()方法让新创建的线程优先完成
35                 thread.join();
36             }catch (InterruptedException e){
37                 e.printStackTrace();
38             }
39         }
40     }
41 }
View Code

运行结果图

技术图片

4、InetAdress类的常用方法

技术图片

技术图片
 1 package code0328;
 2 
 3 import java.net.InetAddress;
 4 import java.net.UnknownHostException;
 5 
 6 public class Adress {
 7     public static void main(String[] args) {
 8         //声明InetAddress对象
 9         InetAddress ip;
10         //try预警捕捉可能出现的异常
11         try {
12             //实例化对象
13             ip = InetAddress.getLocalHost();
14             //获取本地主机名
15             String Localname = ip.getHostName();
16             //获取本地主机的IP地址
17             String localip = ip.getHostAddress();
18             //输出本地主机名
19             System.out.println("本地主机名:" + Localname);
20             //输出本地主机的IP地址
21             System.out.println("本地ip地址:"+localip);
22         }catch (UnknownHostException e){
23             //输出异常
24             e.printStackTrace();
25         }
26     }
27 }
View Code

 运行结果图

技术图片

 5、数组工具类Arrays

技术图片

技术图片
 1 package cn.intcast.demo14;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Code01Arrays {
 6     public static void main(String[] args) {
 7         int[] intArray = {1,2,3,4,5};
 8         //将int[]数组按照默认格式变成字符串
 9         String inStr = Arrays.toString(intArray);
10         System.out.println(inStr);
11 
12         int[] array1 = {11,23,4,2,1,10};
13         Arrays.sort(array1);
14         System.out.println(Arrays.toString(array1));
15 
16         String[] array2 = {"o","z","d","s","a"};
17         Arrays.sort(array2);
18         System.out.println(Arrays.toString(array2));
19     }
20 
21 }
View Code

 运行结果图

技术图片

6、字符串倒叙排列

技术图片
 1 package cn.intcast.demo14;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Code01Arrays {
 6     public static void main(String[] args) {
 7         String intArray = "qweraszxc";
 8         char[] chars = intArray.toCharArray();
 9         Arrays.sort(chars);
10         for (int i = chars.length - 1; i >= 0; i--) {
11             System.out.print(chars[i]+",");
12         }
13 
14 
15     }
16 
17 }
View Code

 运行结果图

技术图片

 

JAVA进阶14

标签:进度   extends   rar   tac   alt   常用   info   exception   java.awt   

原文地址:https://www.cnblogs.com/Anemia-BOY/p/10615373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!