Skip to content

Instantly share code, notes, and snippets.

@diaolizhi
diaolizhi / LazySingleton.java
Created Nov 13, 2018
Java 懒汉式单例模式 - 线程安全
View LazySingleton.java
package chapter3;
/**
* @program: studythread2
* @description: 懒汉式单例模式
* @author: diaolizhi
* @create: 2018-11-13 12:39
**/
public class LazySingleton {
@diaolizhi
diaolizhi / SynchronizedDemo.java
Created Nov 13, 2018
Java synchronized 作用于普通方法、静态方法、方法块
View SynchronizedDemo.java
package chapter3;
/**
* @program: studythread2
* @description: synchronized Demo
* @author: diaolizhi
* @create: 2018-11-13 10:29
**/
public class SynchronizedDemo {
@diaolizhi
diaolizhi / note.java
Created Oct 28, 2018
Android 跳转到一个 Activity 前清空 Activity 栈
View note.java
btnFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentToFinish = new Intent(WizardThree.this, FinishActivity.class);
//重点是下面这一句
intentToFinish.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentToFinish.putExtra("setting", setting);
startActivity(intentToFinish);
WizardThree.this.finish();
}
@diaolizhi
diaolizhi / Activity.java
Created Oct 28, 2018
Android Intent 传递对象
View Activity.java
//
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, WizardOne.class);
intent.putExtra("setting", setting);
startActivityForResult(intent, 1);
}
});
@diaolizhi
diaolizhi / timer.cs
Last active Oct 25, 2018
C# 定时回调委托
View timer.cs
public Form1()
{
//TimerCallback 是委托类型,可以关联一个形参为 Object 的函数
TimerCallback timerCallback = new TimerCallback(timeUpdate);
//Timer 每隔 1000ms 调用委托
System.Threading.Timer timer = new System.Threading.Timer(timerCallback, "", 0, 1000);
}
public void timeUpdate(Object obj)
@diaolizhi
diaolizhi / getcookies.cs
Created Oct 23, 2018
C# WebBrowser 获取 HttpOnly 的 cookie
View getcookies.cs
//不知道为什么有些 cookie 获取不到,只能通过这种方式解决
//这一次的用法是,先在 WebBrowser 登录贴吧网页版,然后将 http://tieba.baidu.com/mo/ 传给 GetCookie 方法
//就可以获取到所有 cookie
//需要引入 System.Net 并 using System.Net;
//下面这段代码作为内部类,如何使用在后面
internal static class CookieReader
{
/// <summary>
@diaolizhi
diaolizhi / random.java
Last active Oct 22, 2018
Java 数组随机打乱顺序
View random.java
public static void shuffle(Object[] a) {
//判断数组 a 是否可以转为整型数组
validateNotNull(a);
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + uniform(n-i); // between i and n-1
Object temp = a[i];
a[i] = a[r];
a[r] = temp;
}
@diaolizhi
diaolizhi / close.cs
Created Oct 21, 2018
C# winform 关闭窗口前确认
View close.cs
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (MessageBox.Show("Sure?",
"Exit",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes);
DialogResult result = MessageBox.Show("提示", "确定退出吗?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
@diaolizhi
diaolizhi / Form2.cs
Created Oct 17, 2018
c# 单例窗口及其关闭处理
View Form2.cs
public partial class Form2 : Form
{
//唯一实例
private static Form2 obj = null;
private Form2()
{
//初始化控件(自动生成)
InitializeComponent();
}
@diaolizhi
diaolizhi / Singleton.cs
Last active Oct 17, 2018
c# 一般情况下的单例模式
View Singleton.cs
class OnlyYou
{
//构造函数
private OnlyYou()
{
}
//注意类型是 OnlyYou,并且是静态的
private static OnlyYou obj = null;
You can’t perform that action at this time.