Programming
Articles, notes, and tutorials filed under Programming.
Latest articles
Showing 9-16 of 17Difference between wait() and sleep(), yield() in Java
Differences between wait() and sleep() or sleep() and yield() method in Java Thread is one of the very old questions asked in Java interviews. Though both wait and sleep put threads to the waiting state, they are completely different in terms of behavior and use cases. JavaDoc Definition Thread Class public static void sleep(long millis[, int nanos]) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.
Object Initialization Order in Java
An object is a chunk of memory bundled with the code that manipulates memory. In the memory, the object maintains its state (the values of its instance variables), which can change and evolve throughout its lifetime. To get a newly-created object off to a good start, its newly-allocated memory must be initialized to a proper initial state. Here we take an in-depth look at the mechanisms Java uses to manage object initialization.
HashMap Internal Implementation Analysis in Java
Most of you will agree that HashMap is a most favorite topic for discussion in interviews nowadays. Now, I am continuing this discussion with you all. I am assuming that if you are interested in the internal working of HashMap, you already know the basics of HashMap, so I’m skipping that part. But if you are new to the concept, follow official java docs. Single Statement Answer If anybody asks me to describe “How HashMap works?”, I simply answer: “On the principle of Hashing”. As simple as it is. Now before answering it, one must be very sure to know at least the basics of Hashing. Right?
How To Convert Byte[] Array To String in Java
In some cases, we have to convert the String variable into a Byte array format, for example, JCE encryption. However, how do we convert a Byte[] array to a String afterward? Simple toString() function like the following code is not a working property. It will not display the original text but the byte value. String s = bytes.toString(); In order to convert the Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.
Use SQLite in Qt
Project file (.pro) Add the following line to your .pro file QT += sql Header #include <QtSql/QSqlDatabase> #include <QtSql/QSqlError> class DatabaseManager { public: DatabaseManager(); ~DatabaseManager(); public: bool openDB(); bool deleteDB(); QSqlError lastError(); private: QSqlDatabase db; }; Source bool DatabaseManager::openDB() { // Find QSLite driver db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("database_name_here"); // Open databasee return db.open(); } QSqlError DatabaseManager::lastError() { // If opening database has failed user can ask // error description by QSqlError::text() return db.lastError(); } bool DatabaseManager::deleteDB() { // Close database db.close(); return QFile::remove("database_name_here"); } Execute a query QSqlQuery query; // Create a new table query.exec("CREATE TABLE filelist(fullpath VARCHAR PRIMARY KEY, timestamp VARCHAR)"); // Prepare file Information to insert QFileInfo currentFile = ...; QString filepath = currentFile.absoluteFilePath(); QString timestamp = currentFile.lastModified().date().toString("yyyy-MM"); // Insert a record to the database query.exec("INSERT INTO filelist VALUES('" + filepath + "', '" + timestamp + "')"); // You can also insert a record as following query.prepare("INSERT INTO filelist VALUES(?, ?)"); query.addBindValue(filepath); query.addBindValue(timestamp); query.exec(); query.exec("DROP TABLE filelist");
Convert Excel Date into Timestamp in PHP
Excel stores a date internally as a number of days since January 1, 1900. For example: “June 9th, 2011 10:30 AM” would be stored as “40703.4375”. 40703 is the number of full days since 01/01/1900 and 0.4375 represents the time (10.5/24 = 0.4375). When you process dates read from an Excel spreadsheet (e.g. using PHPExcel) you often want to convert them into a UNIX timestamp i.e. a number of seconds elapsed since midnight of January 1, 1970 UTC.
Writing Your First MFC Application with C++
Welcome to the first windows tutorial. In this tutorial, I’ll give you the basic information on how to code in windows and we’ll go over a few important details first. The basic windows program has two important functions inside. The first one is the event handler(I’ll talk about this one later on) and the second one is the main function(WinMain from now on). WinMain is similar to DOS’ main function, in fact, WinMain is called from the DOS main function but in windows, we don’t have access to the DOS main; everything is covered up by windows. Windows introduces us to messages. A message could be easily described as a pile of data that gets sent to the event handler which then tells the window what to do. A message’s name is prefixed with WM(stands for Windows Message as you might have guessed?). There are a whole lot of messages that a window can handle, however, we’ll only need a few of them. You can use these messages in many ways. For example, the message WM_CREATE, can be used for initialization purposes, it gets sent to the window when it’s created, and WM_PAINT is used when you want to draw something on the screen, and so on, you get the image. Most of the messages get sent to the window automatically, as example when you move your mouse around you send a message to windows, and any other things such as that send messages to windows. You can also send a message to your window yourself, but you’ll probably never need to.
C# - Run Application as Administrator Automatically
In this tutorial, you will learn how you can run your application as an administrator automatically using Visual C#2012. Start by opening your C# Windows forms application. Right click on your project name in the solution explorer and add a new item… Select an application manifest file: Click add. A new tab should open up, most of the codes in it are not important to us. There is one line however that we need to change: