Thread的用法
public class MyThread {
public static void main(String[] args) {
Thread thread = Thread.currentThread();
//这个方法返回的是当前线程所在线程组以及这个线程组的子线程组内活动的线程数
//这个值是一个估计值,所以这个方法的应用场景不大
int activeCount = Thread.activeCount();
System.out.println("当前系统中活动线程数["+activeCount+"]");
//向标准错误输出流输出当前的线程栈,不会阻断程序的继续执行
Thread.dumpStack();
//获取所有线程栈信息
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
//获取类加载器
ClassLoader contextClassLoader = thread.getContextClassLoader();
//获取当前线程名字
String threadName = thread.getName();
System.out.println("current thread name["+threadName+"]");
//获取当前线程ID
long threadId = thread.getId();
System.out.println("current thread id["+threadId+"]");
//获取当前线程的优先级,一共有1~10总共10个优先级,这个优先级并不是在
//所有平台都生效的
int priority = thread.getPriority();
System.out.println("current thread priority["+priority+"]");
StackTraceElement[] stackTrace = thread.getStackTrace();
System.out.println("-------------stackTrace info--------------");
for (int i = 0; i < stackTrace.length; i++) {
StackTraceElement element = stackTrace[i];
System.out.println("className:["+element.getClassName()+"]");
System.out.println("fileName:["+element.getFileName()+"]");
System.out.println("line nunber:["+element.getLineNumber()+"]");
System.out.println("method name:["+element.getMethodName()+"]");
System.out.println("is native method:["+element.isNativeMethod()+"]");
System.out.println("------------------------------------------");
}
Thread.State state = thread.getState();
System.out.println("thread state:["+state+"]");
ThreadGroup threadGroup = thread.getThreadGroup();
String threadGroupName = threadGroup.getName();
System.out.println("thread group name:["+threadGroupName+"]");
//线程睡眠,调用sleep方法会使得线程进入timed_waiting状态,如果线程已经
//获得了锁资源,调用sleep方法是不会释放这个锁的
Thread.sleep(2000,500);
Thread.sleep(1000);
TimeUnit.SECONDS.sleep(2);
Thread thread1 = new Thread(new Runnable() {
@SneakyThrows
@Override
public void run() {
TimeUnit.SECONDS.sleep(100);
}
});
thread1.start();
thread1.join(50);
}
}
命令行版《2048》
using System;
using System.Collections.Generic;
namespace CSharp_2048
{
class Program
{
static void Main(string[] args)
{
Class2048 class2048 = new Class2048();
class2048.GameStart();
}
/// <summary>
/// 游戏类2048
/// </summary>
class Class2048
{
public int[,] arr = new int[4, 4];
public Random rd = new Random();
public List<CoordinateTools> listOfCoo = new List<CoordinateTools>();
/// <summary>
/// 输出当前状态
/// </summary>
public void Output()
{
string str = " ";
Console.Clear();
Console.WriteLine("┏┉┉┉┉┉┉┉┉┳┉┉┉┉┉┉┉┉┳┉┉┉┉┉┉┉┉┳┉┉┉┉┉┉┉┉┓");
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┋ {0} ┋ {1} ┋ {2} ┋ {3} ┋",
arr[0, 0] == 0 ? str : arr[0, 0].ToString().PadLeft(4, ' '),
arr[0, 1] == 0 ? str : arr[0, 1].ToString().PadLeft(4, ' '),
arr[0, 2] == 0 ? str : arr[0, 2].ToString().PadLeft(4, ' '),
arr[0, 3] == 0 ? str : arr[0, 3].ToString().PadLeft(4, ' '));
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┣┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉┫");
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┋ {0} ┋ {1} ┋ {2} ┋ {3} ┋",
arr[1, 0] == 0 ? str : arr[1, 0].ToString().PadLeft(4, ' '),
arr[1, 1] == 0 ? str : arr[1, 1].ToString().PadLeft(4, ' '),
arr[1, 2] == 0 ? str : arr[1, 2].ToString().PadLeft(4, ' '),
arr[1, 3] == 0 ? str : arr[1, 3].ToString().PadLeft(4, ' '));
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┣┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉┫");
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┋ {0} ┋ {1} ┋ {2} ┋ {3} ┋",
arr[2, 0] == 0 ? str : arr[2, 0].ToString().PadLeft(4, ' '),
arr[2, 1] == 0 ? str : arr[2, 1].ToString().PadLeft(4, ' '),
arr[2, 2] == 0 ? str : arr[2, 2].ToString().PadLeft(4, ' '),
arr[2, 3] == 0 ? str : arr[2, 3].ToString().PadLeft(4, ' '));
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┣┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉╋┉┉┉┉┉┉┉┉┫");
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┋ {0} ┋ {1} ┋ {2} ┋ {3} ┋",
arr[3, 0] == 0 ? str : arr[3, 0].ToString().PadLeft(4, ' '),
arr[3, 1] == 0 ? str : arr[3, 1].ToString().PadLeft(4, ' '),
arr[3, 2] == 0 ? str : arr[3, 2].ToString().PadLeft(4, ' '),
arr[3, 3] == 0 ? str : arr[3, 3].ToString().PadLeft(4, ' '));
Console.WriteLine("┋ ┋ ┋ ┋ ┋");
Console.WriteLine("┗┉┉┉┉┉┉┉┉┻┉┉┉┉┉┉┉┉┻┉┉┉┉┉┉┉┉┻┉┉┉┉┉┉┉┉┛");
Console.WriteLine("\n<<命令行版2048>> 请按上下左右(↑←↓→)方向键操作");
}
/// <summary>
/// 遍历非零元素 随机把一个赋为2
/// </summary>
public void Add2()
{
listOfCoo.Clear();
// 遍历所有零元素的坐标
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (arr[i, j] == 0)
{
// 把遍历到的坐标 当成参数 实例化
CoordinateTools coo = new CoordinateTools(i, j);
// 把实例化的结果add到list里
listOfCoo.Add(coo);
}
}
}
// 如果列表里一个元素都没存进来 说明表里没有空格了 直接退出
if (listOfCoo.Count == 0)
{
return;
}
// 从表里随机取一个位置
int cooPlus = rd.Next(0, listOfCoo.Count);
// 把这个位置赋值改写为2
arr[listOfCoo[cooPlus].x, listOfCoo[cooPlus].y] = 2;
}
/// <summary>
/// 游戏开始
/// </summary>
public void GameStart()
{
Add2();
Add2();
Output();
while (true)
{
// 用于遍历检测按下按键之后和之前有没有区别用的bool型变量
bool flag = false;
// 胜利条件 遍历
foreach (int item in arr)
{
if (item == 2048)
{
Console.WriteLine("\n(ノ´▽`)ノ♪ ------ 游戏胜利 ------ (ノ´▽`)ノ♪");
Last();
}
}
// 这是用于检测按下按键之后和之前有没有区别用的备份数组
int[,] arrtmp = new int[4, 4];
// 遍历给备份数组赋值
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arrtmp[i, j] = arr[i, j];
}
}
// 获取用户操作 --> 上下左右
ConsoleKeyInfo info = Console.ReadKey(true);
switch (info.Key)
{
case ConsoleKey.UpArrow:
MoveUp();
break;
case ConsoleKey.DownArrow:
MoveDown();
break;
case ConsoleKey.LeftArrow:
MoveLeft();
break;
case ConsoleKey.RightArrow:
MoveRight();
break;
}
// 遍历检测 按下方向键前的状态 和 按下方向键之后的状态是不是完全一样的
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (arrtmp[i, j] != arr[i, j])
{
// 一旦有任意一个元素在之前之后不一样 那么falg改为true
flag = true;
}
}
}
if (flag)
{
// 如果falg是true 说明变了, 如果变了 就刷一个2出来,
// 反之就什么也不干
Add2();
}
// 输出到控制台
Output();
// 检测按下方向键之后死没死
if (!End())
{
Console.WriteLine("\n(;´д`)ゞ ------ 游戏失败 ------ (;´д`)ゞ");
Last();
}
}
}
#region 核心逻辑 --> 向四个方向移动的控制
/// <summary>
/// 向下 非0数向下移动,遇到非0数,相同则累加,不同则保存到当前位置
/// </summary>
public void MoveDown()
{
for (int j = 0; j < 4; j++)
{
for (int i = 2; i >= 0; i--)
{
if (arr[i, j] == 0) continue;
for (int k = i + 1; k < 4; k++)
{
if (arr[k, j] != 0)
{
if (arr[i, j] == arr[k, j])
{
arr[k, j] += arr[i, j];
arr[i, j] = 0;
break;
}
else if (arr[i, j] != arr[k, j] && k - 1 != i)
{
arr[k - 1, j] = arr[i, j];
arr[i, j] = 0;
break;
}
else if (arr[i, j] != arr[k, j] && k - 1 == i)
{
break;
}
}
if (k == 3)
{
arr[k, j] = arr[i, j];
arr[i, j] = 0;
break;
}
}
}
}
}
/// <summary>
/// 向上移动: 先把数组上下翻转 然后向下移动 移动完了再翻转回来
/// </summary>
public void MoveUp()
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
int tmp = 0;
tmp = arr[i, j];
arr[i, j] = arr[3 - i, j];
arr[3 - i, j] = tmp;
}
}
MoveDown();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
int tmp = 0;
tmp = arr[i, j];
arr[i, j] = arr[3 - i, j];
arr[3 - i, j] = tmp;
}
}
}
/// <summary>
/// 向左移动
/// </summary>
public void MoveLeft()
{
for (int i = 0; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
if (arr[i, j] == 0) continue;
for (int k = j - 1; k >= 0; k--)
{
if (arr[i, k] != 0)
{
if (arr[i, j] == arr[i, k])
{
arr[i, k] += arr[i, j];
arr[i, j] = 0;
break;
}
else if (arr[i, j] != arr[i, k] && k + 1 != j)
{
arr[i, k + 1] = arr[i, j];
arr[i, j] = 0;
break;
}
else if (arr[i, j] != arr[i, k] && k + 1 == j)
{
break;
}
}
if (k == 0)
{
arr[i, k] = arr[i, j];
arr[i, j] = 0;
break;
}
}
}
}
}
/// <summary>
/// 向右移动: 先把数组左右翻转 然后向左移动 移动完了再翻转回来
/// </summary>
public void MoveRight()
{
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < 4; i++)
{
int tmp = 0;
tmp = arr[i, j];
arr[i, j] = arr[i, 3 - j];
arr[i, 3 - j] = tmp;
}
}
MoveLeft();
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < 4; i++)
{
int tmp = 0;
tmp = arr[i, j];
arr[i, j] = arr[i, 3 - j];
arr[i, 3 - j] = tmp;
}
}
}
#endregion
/// <summary>
/// 判断是否失败
/// </summary>
/// <returns></returns>
public bool End()
{
// 遍历数组 有任何一个空元素都说明不可能死
foreach (int item in arr)
{
if (item == 0)
return true;
}
// 从2开始到2048进行遍历
// 目的是检测 每一个数字 他上下左右相邻有没有和他一样的数字
for (int num = 2; num <= 2048; num *= 2)
{
List<CoordinateTools> listOfget2 = new List<CoordinateTools>();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (arr[i, j] == num)
{
// 先把所有值为NUM的元素的下标 存到list里
CoordinateTools coo = new CoordinateTools(i, j);
listOfget2.Add(coo);
}
}
}
// 如果这个list 是空的 就说明当前表里没有num 回到FOR继续
if (listOfget2 == null)
{
continue;
}
// 从列表里的第一个元素开始 (每一个元素存的都是一组下标x,y)
foreach (CoordinateTools item in listOfget2)
{
foreach (CoordinateTools item2 in listOfget2)
{
// 判断 同一行的是不是列坐标差的绝对值是1 同一列的是不是行坐标差的绝对值是1
if ((item.y == item2.y && Math.Abs(item.x - item2.x) == 1) ||
(item.x == item2.x && Math.Abs(item.y - item2.y) == 1))
{
// 如果有一个 就不用再循环了 肯定没死
return true;
}
}
}
}
// 全遍历完了 就说明已经死了 返回false
return false;
}
/// <summary>
/// 胜利或失败之后的选择
/// </summary>
public void Last()
{
Console.WriteLine("\n输入X退出 输入R重新开始\n");
while (true)
{
string str = Console.ReadLine();
if (str == "x")
{
Environment.Exit(0);
}
//重新开始 --> 初始化
if (str == "r")
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arr[i, j] = 0;
}
}
GameStart();
}
}
}
}
/// <summary>
/// 工具类 用于存储搜索到的数组的下标
/// </summary>
class CoordinateTools
{
public int x { set; get; }
public int y { set; get; }
public CoordinateTools(int i, int j)
{
this.x = i;
this.y = j;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace jtxh
{
enum 信号灯
{
无,
红,
绿,
黄,
}
class Program
{
//static void Simple()
//{
// while (true)
// {
// Console.Clear();
// Console.WriteLine("绿灯");
// //等待3秒
// Thread.Sleep(3000);
// Console.Clear();
// Console.WriteLine("黄灯");
// //等待0.4秒
// Thread.Sleep(400);
// Console.Clear();
// Console.WriteLine("红灯");
// //等待3秒
// Thread.Sleep(3000);
// }
//}
static void Main(string[] args)
{
信号灯 state = 信号灯.无;
int time = 0;
while (true)
{
switch (state)
{
case 信号灯.红:
{
Console.Clear();
Console.WriteLine("红灯");
time--;
if (time == 0)
{
state = 信号灯.绿;
time = 30;
}
}
break;
case 信号灯.绿:
{
Console.Clear();
Console.WriteLine("绿灯");
time--;
if (time == 0)
{
state = 信号灯.黄;
time = 30;
}
}
break;
case 信号灯.黄:
{
Console.Clear();
Console.WriteLine("黄灯");
time--;
if (time == 0)
{
state = 信号灯.红;
time = 4;
}
}
break;
default:
{
state = 信号灯.红;
time = 30;
}
break;
}
Thread.Sleep(100);
}
}
}
}