using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class SingleModle {
private static SingleModle _singleModle;
private SingleModle() //私有化构造函数,外界无法通过new来实例化该类
{
}
public static SingleModle GetInstance()
{
if (_singleModle == null)
{
_singleModle = new SingleModle ();
}
return _singleModle;
}
public string name = "Likang";
private int level = 6;
private static readonly object obj = new object ();
public Thread threadone;
public Thread threadtwo;
public Queue<int> _queue = new Queue<int>();
public void startThread()
{
threadone = new Thread (SS);
threadone.Start ();
threadtwo = new Thread (SS);
threadtwo.Start ();
}
public void SS()
{
lock (_queue)
{
_queue.Enqueue (5);
Thread.Sleep (5000);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FileWriteAndRead : MonoBehaviour {
private BoxCollider[] cube;
void Start ()
{
SingleModle.GetInstance ().startThread ();
}
void Update ()
{
print (SingleModle.GetInstance()._queue.Count);
}
void OnApplicationQuit()
{
SingleModle.GetInstance().threadone.Abort();
SingleModle.GetInstance ().threadtwo.Abort ();
}
}