Java实现代码等待一段时间后执行

方法一:线程睡眠,根据系统计时器和调度程序的精度和准确性,使当前正在执行的线程进入休眠状态(暂时停止执行)达指定的毫秒数。 该线程不会失去任何监视器的所有权,参数单位是毫秒

Thread.sleep(3000); // 毫秒

// 特殊情况
Thread.currentThread().sleep(1000); // 毫秒

方法二:java.util.concurrent.TimeUnit 类中的 sleep() 方法,底层调用的也是线程睡眠

import java.util.concurrent.TimeUnit;

TimeUnit.DAYS.sleep(1); // 天
TimeUnit.HOURS.sleep(1); // 小时
TimeUnit.MINUTES.sleep(1); // 分
TimeUnit.SECONDS.sleep(1); // 秒
TimeUnit.MILLISECONDS.sleep(1000); // 毫秒
TimeUnit.MICROSECONDS.sleep(1000); // 微妙
TimeUnit.NANOSECONDS.sleep(1000); // 纳秒

报未处理异常: java.lang.InterruptedException?
使用 try/catch 环绕

try{
    TimeUnit.MINUTES.sleep(1);
} catch (InterruptedException e) {
    e.printStackTrace();
}

微信无法登录,扫码后报错:java.lang.NumberFormatException:null

原因:微信需加JVM运行参数

找到你的IDE所在的安装目录下的bin文件夹,打开,找到你的exe运行参数文件,如(idea64.exe.vmoptions,studio64.exe.vmoptions)使用编辑器打开,在最后一行添加

-Djsse.enableSNIExtension=false
// 保存后重启IDE

也可以通过exe快捷方式添加参数,如快捷方式目标指向:

E:\Android\eclipse\eclipse.exe -nl=en -Djsse.enableSNIExtension=false

这种方式要求exe所在的路径不能带空格

*Java系统属性的修改通常只能用于本次运行

将Java系统属性Djsse.enableSNIExtension设置为false,$java -Djsse.enableSNIExtension=false
尝试运行时只输出帮助信息?解决方法:

$java -Djsse.enableSNIExtension=false MainClass
$java -Djsse.enableSNIExtension=false -jar foobar.jar

为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录

<error><ret>1203</ret><message>为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:https://pc.weixin.qq.com  Mac微信下载地址:https://mac.weixin.qq.com</message></error>

好几年的微信老账号,早就实名认证了依然是没办法登录

临时解决方法:

1、微信关联QQ

2、下载 QQ浏览器,安装并用QQ号登录

3、登录成功后,QQ浏览器的左侧有一个工具条,有个微信插件的安装,安装,扫码,登录成功

微信官方已经不再支持登录web协议了

虚拟机病毒会感染电脑吗?

基本就是两个方向,一个方向是虚拟机本身存在漏洞,历史上在18、19年的时候 VMware就出现过这样的一次问题:

VMware Workstation有一个拖放功能,就曾经出现了越界内存访问漏洞,在虚拟机正常情况下应该是访问虚拟的内存,而不访问宿主机(实体机)的内存,但漏洞导致有时候会访问实体机内存,黑客就可以制作一个命令执行。如果虚拟机已经中毒了,病毒就可以逃出虚拟机感染宿主机,在宿主机上执行命令。

第二个方向是在自己不知道虚拟机已经被病毒感染的情况下,从虚拟机里把文件拷贝出来,存储到了宿主机的硬盘中,就会导致电脑被感染。

除去以上两种情况,虚拟机还是很安全的,在里边安装杀毒软件后再下载软件,出现的问题就不会很严重了。

在虚拟机安全中,有个专业的名词就叫做虚拟机逃逸,指的是进程越过虚拟机范围,进入到宿主机的操作系统中,也就是病毒逃离虚拟机,对真实主机造成危害。

Pwn2Own黑客大赛:虚拟机逃逸获得10500美元奖金——通过三个独立漏洞的巧妙利用
在加拿大温哥华的Pwn2Own世界黑客大赛上,来自360安全战队的参赛选手完成了一项令人印象极其深刻的战绩:他们完成了虚拟化软件VMware Workstation的逃逸,并攻破了微软防御工事森严的Edge浏览器。这次破解给他们带来了10.5万美元的奖励,这是Pwn2Own连续三天赛程上的最高奖励。
根据Pwn2Own赛事主办方在周五早上(加拿大当地时间)发布的Tweet显示,来自360安全战队的成员运用了Edge的堆溢出漏洞、微软内核的一种混乱缺陷、VMware的缓冲区漏洞,成功完成了“完整的虚拟机逃逸过程”。

“实际上,虚拟机管理程序就是另一个存在漏洞,且可以被攻破的软件隔离层。”Capsule8的联合创始人兼首席技术官Dino Dai Zovi告诉记者,Capsule8是为现代网络基础设施提供实时威胁防护的公司。“隔离层比如沙箱、虚拟化、封装等都让黑客的攻击难度增加,但是没有一个隔离层是完美的。作为防御者来说,应该始终保持这样的态度,那就是所有的防御措施都可以通过攻击者的足够努力而被攻破。”报道链接:https://arstechnica.com/security/2017/03/hack-that-escapes-vm-by-exploiting-edge-browser-fetches-105000-at-pwn2own/

String类valueOf(int i)方法的实现

Java 库 String 类的 valueOf 方法大致可以分为三种:

  • String.valueOf(Object)
  • String.valueOf(char[])
  • String.valueOf(基本数据类型)


String.valueOf(int i)源码:

public static String valueOf(int i) {
        return Integer.toString(i);
}


Integer.toString(int i)源码:

public static String toString(int i) {
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        char[] buf = new char[size];
        getChars(i, size, buf);
        return new String(buf, true);
}


Integer.getChars(int i, int index, char[] buf)的源码:

static void getChars(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Generate two digits per iteration
        while (i >= 65536) {
            q = i / 100;
        // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
            buf [--charPos] = DigitOnes[r];
            buf [--charPos] = DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) {
            q = (i * 52429) >>> (16+3);
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
            buf [--charPos] = digits [r];
            i = q;
            if (i == 0) break;
        }
        if (sign != 0) {
            buf [--charPos] = sign;
        }
    }

其他方式实现MyString1 valueOf(int i)

public static MyString1 valueOf(int i) {
        int n;
        char[] temp;
        int count;

        if (i <= Character.MAX_VALUE)
            return new MyString1(Character.toChars(i));
        else {
            n = i % Character.MAX_VALUE;
            count = i / Character.MAX_VALUE;
            temp = new char[count + 1];
            temp[0] = (char) n;
            for (int j = 1; j <= count; j++)
                temp[j] = Character.MAX_VALUE;
            return new MyString1(temp);
        }
    }

刚注册不久没有使用的Facebook账户被封禁

2021/3/8
请上传一张清楚显示你面部的照片。请确保照片足够亮、不模糊,而且其中没有其他人。我们使用此照片来验证这是你的帐户。完成验证后,我们会立即删除照片,决不让这张照片显示在你的个人主页中。

没有分享过照片与视频,包括头像,注册后使用 Facebook 账号登录过第三方网站,这可能是账户被封禁的原因

win10删除文件卡在99%

删除文件时还好,卡在99%进度大约1分钟时间,剪切/移动文件就悲剧了,一直卡
解决方法:
以管理员身份运行“命令提示符”

粘贴以下命令并执行如下
dism /online /Cleanup-Image /RestoreHealth & sfc /SCANNOW & pause

修复成功,但还在卡,只能到任务管理器中重启Windows 资源管理器+关闭Windows Search了,我执行的是移动文件操作,如果强行关闭对话框可能有丢文件的风险,经过测试修复后重启系统问题不再出现,刚才移动的文件也是完整的。

第七章第五题(打印不同的教)(Print distinct numbers) – 编程练习题答案

编写一个程序,读人10 个数并且显示互不相同的数(即一个数出现多次,但仅显示一次)。(提示,读人一个数,如果它是一个新数,则将它存储在数组中。如果该数已经在数组中,则忽略它。)输入之后,数组包含的都是不同的数。下面是这个程序的运行示例:

Write a program that reads in ten numbers and displays
the number of distinct numbers and the distinct numbers separated by exactly one
space (i.e., if a number appears multiple times, it is displayed only once). (Hint:
Read a number and store it to an array if it is new. If the number is already in the
array, ignore it.) After the input, the array contains the distinct numbers.

下面是参考答案代码:

// 缺陷版本
import java.util.Arrays;
import java.util.Scanner;

public class Ans7_5_page236 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter ten numbers: ");
        int[] numberList = new int[10];
        // int[] distinctList = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
        int[] distinctList = new int[10];
        Arrays.fill(distinctList,-1);

        for (int i = 0; i < 10; i++)
            numberList[i] = input.nextInt();

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10;j++) {
                if (i == numberList[j])
                    distinctList[i] = numberList[j];
            }
        }

        int count = 0;
        for (int i = 0; i < 10; i++) {
            if (distinctList[i] != -1)
                count++;
        }
        System.out.print("The number of distinct number is " + count+
                "\nThe distinct numbers are: ");

        for (int i = 0; i < 10; i++) {
            if (distinctList[i] != -1) {
                System.out.print(distinctList[i] + " ");
            }
        }
    }
}

// 完善版本
import java.util.Arrays;
import java.util.Scanner;

public class Ans7_5_page236 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter ten numbers: ");
        int[] numberList = new int[10];

        int maxInt = -99;
        for (int i = 0; i < 10; i++) {
            numberList[i] = input.nextInt();
            if (numberList[i] > maxInt)
                maxInt = numberList[i];
        }

        int[] distinctList = new int[maxInt];
        Arrays.fill(distinctList, -99);

        int disNum = 0;
        for (int i = 0; i < maxInt; i++) {
            for (int j = 0; j < 10; j++) {
                if (i == numberList[j])
                    distinctList[i] = numberList[j];
                else if (distinctList[i] != -99)
                    disNum = distinctList[i];
            }
        }

        int count = 0;
        for (int i = 0; i < maxInt; i++) {
            if (distinctList[i] != -99) {
                count++;
            }
        }
        System.out.print("The number of distinct number is " + disNum +
                "\nThe distinct numbers are: ");

        for (int i = 0; i < maxInt; i++) {
            if (distinctList[i] != -99) {
                System.out.print(distinctList[i] + " ");
            }
        }
        System.out.print(maxInt);
    }
}

//        Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
//        The number of distinct number is 5
//        The distinct numbers are: 1 2 3 4 5 6

//        Enter ten numbers: 11 22 33 22 11 66 33 44 55 67
//        The number of distinct number is 66
//        The distinct numbers are: 11 22 33 44 55 66 67

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)

发布在博客:(https://cn.fankuiba.com)

第七章第四题(分析成绩)(Analyze scores) – 编程练习题答案

编写一个程序,读人个数不确定的考试分数,并且判断有多少个分数是大于或等于平均分,多少个分数是低于平均分的。输人一个负数表示输入的结束。假设最高分为100。

Write a program that reads an unspecified number of scores and
determines how many scores are above or equal to the average and how many
scores are below the average. Enter a negative number to signify the end of the
input. Assume that the maximum number of scores is 100.

下面是参考答案代码:

// https://cn.fankuiba.com
import java.util.Scanner;

public class Ans7_4_page236 {
    public static void main(String[] args) {
        double[] scoreList = new double[100];
        Scanner input = new Scanner(System.in);
        System.out.print("Enter scores: (negative number signifies end): ");
        int count= 0;double score;
        do {
            score = input.nextDouble();
            scoreList[count] = score;
            count++;
        }while (score >= 0);

        double average,sum = 0;
        for (int i = 0; i < count-1; i++)
            sum += scoreList[i];

        average = sum / (count - 1);
        System.out.println("Average of scores: "+average);
        int minAverge = 0;
        int maxAverge = 0;
        for (int i = 0; i < count-1; i++) {
            if (scoreList[i] >= average)
                minAverge++;
            else
                maxAverge++;
        }
        System.out.println("Number of scores above or equal to average: " +minAverge+
                "\n"+"Number of scores below average: "+(maxAverge));
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)

发布在博客:(https://cn.fankuiba.com)

第七章第三题(计算数字的出现次数)(Count occurrence of numbers) – 编程练习题答案

编写程序,读取在1到100 之间的整数,然后计算每个数出现的次数。假定输入是以0 结束的。

下面是这个程序的一个运行示例:

Write a program that reads the integers between 1and 100 and counts the occurrences of each. Assume the input ends with 0.Note that if a number occurs more than one time, the plural word “times” is used

in the output.

Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time

下面是参考答案代码:

// https://cn.fankuiba.com
import java.util.Scanner;

public class Ans7_3_page236 {
    public static void main(String[] args) {
        int[] number = new int [101];
        Scanner input = new Scanner(System.in);
        int num;
        System.out.print("Enter the integers between 1 and 100: ");
        do {
            num = input.nextInt();
            number[num] = number[num] + 1;
        }
        while (num != 0);
        for (int i = 1; i < number.length; i++) {
            if (number[i] == 1) {
                System.out.println(i + " occurs " + number[i] + " time");
            }else if (number[i] > 1)
                System.out.println(i + " occurs " + number[i] + " times");
        }
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)

发布在博客:(https://cn.fankuiba.com)